您的位置:首页 > 移动开发 > Objective-C

Conversion between json and object using SBJson lib

2013-08-20 21:19 417 查看
Define two methods in an object class as follows:

@interface MyObject : NSObject
@property (nonatomic,copy) NSString *property;

- (id)initWithJson:(NSDictionary *)item;
- (id)proxyForJson;

- (id)initWithJson:(NSDictionary *)item {
self = [superinit];
if (self) {
self.property = [itemobjectForKey:@"Property"];
}
return self;
}

- (id) proxyForJson {
return [NSDictionarydictionaryWithObjectsAndKeys:property,@"Property",
nil];
}


Convert json to object

NSData *dataReply = [NSURLConnectionsendSynchronousRequest:urlRequest returningResponse:&response error:&error];

NSDictionary *results = [dataReply JSONValue];

MyObject *object = [[MyObjectalloc] initWithJson:results];


Convert object to json string


MyObject *object = [[MyObjectalloc]init];



NSString*json = [objectJSONRepresentation];





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: