Reputation: 1024
I have a write a simple json representing the position of same places like:
{
lat = "41.653048";
long = "-0.880677";
name = LIMPIA;
},
In iphone programming, my app is able to show the position on the map. My question is about how replicate the same on multiple position. For example with a json like:
{
lat = "41.653048";
long = "-0.890677";
name = name1 ;
},
{
lat = "41.653048";
long = "-0.890677";
name = name2;
},
How i can able to know the number of itemes? I have to change the json adding anything else in representation?or i have to completelin change it?
Upvotes: 0
Views: 227
Reputation: 10585
Not sure if you mean to parse or write JSON from/to objects. I outline both.
@interface MyPointModel : NSObject
@property (nonatomic, strong) NSNumber *latitude;
@property (nonatomic, strong) NSNumber *longitude;
@property (nonatomic, strong) NSString *name;
@end
@implementation MyPointModel
@synthesize latitude, longitude, name;
@end
Parsing
(and where ever you do the parsing)
@implementation ParsingController
...
- (NSArray *)buildArrayOfMyPointModelWithString:(NSString *)json {
NSArray *array = [jsonParser.objectWithString:json error:NULL];
NSMutableArray *arrayOfMyPointModel = [[NSMutableArray alloc] init];
for (id jsonElement in array) {
MyPointModel *m = [[MyPointModel alloc] init];
m.latitude = [jsonElement valueForKey:@"lat"];
m.longitude = [jsonElement valueForKey:@"long"];
m.name = [jsonElement valueForKey:@"name"];
[arrayOfMyPointModel addObject:m];
}
return (NSArray *)arrayOfMyPointModel;
}
Writing
(a sample array of my point)
NSMutableArray *a = [[NSMutableArray alloc] init];
MyPointModel *m1 = [[MyPointModel alloc] init];
MyPointModel *m2 = [[MyPointModel alloc] init];
m1.latitude = [NSNumber numberWithDouble:0.1];
m1.longitude = [NSNumber numberWithDouble:0.01];
m1.name = @"M1";
m2.latitude = [NSNumber numberWithDouble:0.2];
m2.longitude = [NSNumber numberWithDouble:0.02];
m2.name = @"M2";
[a addObject:m1];
[a addObject:m2];
- (NSString *)buildJsonStringWithMyPointModelArray:(NSArray *)array {
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
return [writer stringWithObject:array];
}
Upvotes: 0
Reputation: 23634
I'm not sure I understand your question but it seems like you want to know how to best include multiple points in one JSON object?
{
"locations":
[
{
lat = "41.653048";
long = "-0.890677";
name = name1 ;
},
{
lat = "41.653048";
long = "-0.890677";
name = name2;
}
]
}
Upvotes: 0
Reputation: 181350
You need to use a JSON array:
[
{
lat = "41.653048";
long = "-0.890677";
name = name1 ;
},
{
lat = "41.653048";
long = "-0.890677";
name = name2;
},
]
Upvotes: 1