谁能告诉我如何从下面的JSON响应中解析并提取数据节点,即CM,Dozen,Foot ......,到NSMutableArray,那么它可以用作UITableView的数据源吗?
{success: true, dataCount: 11, data:
[{"unitName":"CM"},{"unitName":"Dozen"},{"unitName":"Foot"},{"unitName":"Gross"},{"unitName":"Inch"},{"unitName":"K"},{"unitName":"KG"},{"unitName":"LB"},{"unitName":"Piece"},{"unitName":"Set"},{"unitName":"Yard"}]}
Thanks in advance for your help.
Cheers
Update...
Oli & Srika, You guys are so helpful!
Below is my code...
NSString *myRawJson = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://192.168.0.100/myApp/getUnitInJson.php"]];
//myRawJson response is like below
{success: true, dataCount: 5, data: [{"unitName":"CM"},{"unitName":"Dozen"},{"unitName":"Foot"},{"unitName":"Gross"},{"unitName":"Inch"}]}
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSArray *myParsedJson = [[parser objectWithString:myRawJson error:nil] copy];
NSArray *myExtractedData = [myParsedJson valueForKey:@"data"];
NSLog(@"RawJsonString: %@", myRawJson);
// ouput: RawJsonString: {data:[{"unitName":"CM"},{"unitName":"Inch"},{"unitName":"Foot"},{"unitName":"Yard"},]}
NSLog(@"myExtractedData Count: %@", [myExtractedData count]);
// output: myExtractedData Count: (null)
myExtractedData中没有任何内容。我做错了吗?
请帮忙。
答案 0 :(得分:0)
从整个data
获取NSDictionary
-
NSArray values = [data objectForKey:@"data"]
。
这会将NSArray
与data
相关联。
让CM试试这个 - NSString *CM = [[values objectAtIndex:0] objectForKey:@"unitName"]
十二英尺也是如此。等
然后,您可以将此数据放入UITableView
。
更新:试试这个。我假设你使用this作为JSON解析器。
NSDictionary *myParsedJson = [myRawJson JSONValue];
NSArray *myExtractedData = [myParsedJson valueForKey:@"data"];
NSLog(@"myExtractedData Count: %@", [myExtractedData count]);
答案 1 :(得分:0)
您是否让用户在UITableView中添加/修改数组,这就是您需要它变为可变的原因吗?
在任何情况下,您都可以这样做:从“wholeResponse”字典访问数据数组,然后将该数组转换为可变数组。
NSArray *data = [wholeResponse valueForKey:@"data"];
NSMutableArray *mutableData = [NSMutableArray arrayWithArray:data];
// Do something with the mutable data.