2011-11-04 109 views
0

Possible Duplicate:
How to parse JSON in iOS AppIPhone解析JSON

我有以下的JSON:

NSString *jsonResult = @"{\"code\":\"000\",\"message\":\"success\",\"datas\":[{\"datas\":[{\"service_name\":\"user_info\",\"name\":\"Account Test 32\",\"number\":123,\"company\":\"ABC\",\"currency\":\"USD\",\"balance\":\"1000\",\"profit\":\"-80.00\",\"credit\":\"0.00\",\"equity\":\"1000.00\",\"leverage\":100,\"free_margin\":\"1000.00\",\"free_margin_mode\":1.0,\"server_name\":\"localhost\",\"stopout_level\":5,\"stopout_mode\":0}],\"service_name\":\"user_info\"}]}"; 

谁能举个例子如何解析呢?

+0

你为什么要解析JSON?这是你从web服务获得的有效载荷吗? – rjgonzo

+0

是的,它是来自Web服务的有效载荷。 – user772315

回答

1

您最好的选择是使用JSON解析库,如TouchJson

2

试试这个。进口SBJson parser你也可以使用TouchJson,但我更喜欢SBJson。

#import "JSON.h" 

// Create SBJSON object to parse JSON 
SBJSON *parser = [[SBJSON alloc] init]; 

// parse the JSON string into an object - assuming json_string is a NSString of JSON data 
NSDictionary *object = [parser objectWithString:jsonResult error:nil]; 
+0

我如何获得数据数组内的值? – user772315

+0

你有'jsonResult'吗?将其传递给JSON解析器。它会转换为ObjectiveC对象...这就是你需要的对吗?所以我写的是解析器部分。你在哪里导入json解析器&解析字符串.... –

+0

它仍然给我一个错误:SBJsonParser * json = [[SBJsonParser alloc] init]; NSError * jsonError; NSDictionary * parsedJSON = [json objectWithString:jsonResult error:&jsonError]; NSDictionary * jsonValue = [parsedJSON objectForKey:@“code”]; NSLog(@“code:%@”,[jsonValue objectForKey:@“code”]); – user772315