Objective c 解析JSON结果时出现问题
我在解析JSON结果时遇到问题。这是我从HTTP请求中得到的:Objective c 解析JSON结果时出现问题,objective-c,json,Objective C,Json,我在解析JSON结果时遇到问题。这是我从HTTP请求中得到的: {"subscriptions": [ {"id":"A", "title":"A title"}, {"id":"B", "title":"B title"}, ]} 这就是我在代码中所做的: // Getting the result<br> NSString *str = [[NSString alloc] initWithData:receivedData encoding:NSAS
{"subscriptions": [
{"id":"A", "title":"A title"},
{"id":"B", "title":"B title"},
]}
这就是我在代码中所做的:
// Getting the result<br>
NSString *str = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
// Creating the JSON parser<br>
SBJSON *parser = [[SBJSON alloc] init];
// Parse result in an object<br>
NSDictionary *result = [parser objectWithString:str];
//获取结果
NSString*str=[[NSString alloc]initWithData:receivedData编码:NSASCIIStringEncoding];
//创建JSON解析器
SBJSON*解析器=[[SBJSON alloc]init];
//分析对象中的结果
NSDictionary*result=[parser objectWithString:str];
到目前为止一切正常。我的result对象中有一个键/值对,我认为这就是subscriptions对象。但现在的问题是:我如何访问它的内部对象,比如id和title
感谢您的帮助。JSON解析器将为您创建嵌套的NSArray和NSDictionary对象。要访问阵列,请使用:
NSArray *array = [result objectForKey:@"subscriptions"];
然后访问数组中的对象,如下所示:
NSDictionary *arrayObject = [array objectForIndex:0];
最后,要获得一个内部对象,请执行以下操作:
NSString *stringObject = [arrayObject objectForKey:@"id"];
您的JSON无效。
,
是分隔符,而不是终止符。(看起来您的解析器正在纠错,但您应该修复源数据)您好,David,感谢您的快速响应。我从谷歌那里得到了这个JSON,所以我认为这不应该是问题所在。