不确定您是否要从/向对象解析或写入 JSON。我概述了两者。
@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
解析
(以及您在哪里进行解析)
@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;
}
写作
(我的观点的样本数组)
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];
}