我想用这种方式解析这个json
{"img":[{"id":"44","name":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","type":"\u0627\u0646\u0627 \u0627\u062a\u0639\u0644\u0645","img":"27039_01355548242.png","school":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","isshow":"1","tarteb":"0","date":"29\/1\/2015 01:15","mob":"16544541321"}]}
我写了这段代码来解析它
NSArray * array = [[NSArray alloc]init];
array = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
NSDictionary * dic =array[indexPath.row]; //here is error
NSLog(@"%@",dic);
NSDictionary * info = dic[@"img"];
cell.name.text = info[@"name"];
cell.date.text =info[@"date"];
cell.schoolName.text= info[@"school"];
cell.mobile.text= info[@"mob"];
然后我收到了这个错误
[__NSCFDictionary objectAtIndex :]: unrecognized selector sent to instance 0x7c986db0
答案 0 :(得分:0)
NSDictionary *jsonDictionary = [[NSDictionary alloc] init];
jsonDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
NSArray *imageArray = jsonDictionary[@"img"];
NSDictionary *firstObject = imageArray[0];
cell.name.text = firstObject[@"name"];
cell.date.text =firstObject[@"date"];
cell.schoolName.text= firstObject[@"school"];
cell.mobile.text= firstObject[@"mob"];
并且如果json中有多行,则为:
{"img":[{"id":"44","name":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","type":"\u0627\u0646\u0627 \u0627\u062a\u0639\u0644\u0645","img":"27039_01355548242.png","school":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","isshow":"1","tarteb":"0","date":"29\/1\/2015 01:15","mob":"16544541321"},{"id":"45","name":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","type":"\u0627\u0646\u0627 \u0627\u062a\u0639\u0644\u0645","img":"27039_01355548242.png","school":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","isshow":"1","tarteb":"0","date":"29\/1\/2015 01:15","mob":"16544541321"},{"id":"46","name":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","type":"\u0627\u0646\u0627 \u0627\u062a\u0639\u0644\u0645","img":"27039_01355548242.png","school":"\u0625\u0628\u0631\u0627\u0647\u064a\u0645 \u0627\u0644\u0639\u0631\u064a\u0641\u064a","isshow":"1","tarteb":"0","date":"29\/1\/2015 01:15","mob":"16544541321"}]}
然后使用此方法,如果您需要在第0行显示id:44
,在第一行显示id:45
,在第二行显示id:46
,...表格视图。
//In View Did Load
NSMutableArray *tableDataSource = [NSMutableArray new];
for (int i = 0; i < imageArray.count; i++)
{
NSDictionary *rowDataDictionary = imageArray[i];
[tableDataSource addObject:rowDataDictionary];
}
//In Cell For Row
NSDictionary *rowData = tableDataSource[indexPath.row];
cell.name.text = rowData[@"name"];
cell.date.text =rowData[@"date"];
cell.schoolName.text= rowData[@"school"];
cell.mobile.text= rowData[@"mob"];
JsonObjectWithData
返回Dictionary,你应该根据JSON的结构解析数据。
希望这有助于,
答案 1 :(得分:0)
如上所示,JsonObjectWithData返回NSDictionary对象,您应该使用valueForKey方法获取所需的值。
NSDictionary *jsonDictionary = [[NSDictionary alloc] init];
jsonDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
NSMutableArray *valueForImgLabel = [jsonDictionary valueForKey:@"img"];
如果您的JSON Response对象中的一个元素的值太大,就像这样;
{
Name = "Mr. Hope";
Gender = "Male";
Age = 29;
ImageList = (
{
Name = "Front side";
Image = "11111.JPG";
Order = 1;
Date = "07/07/2014";
},
{
Name = "Left Side";
Image = "22222.JPG";
Order = 1;
Date = "10/28/2015";
});
}
你可以像这样使用valueForkey转换所有ImageList元素;
NSMutableArray *imageListInJSONFile = [jsonDictionary valueForKey:@"ImageList"][0];