簡體   English   中英

在iOS中解析JSON字典數組

[英]Parse array of dictionaries of json in ios

我想用這種方式解析這個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
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: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的結構解析數據。

希望這可以幫助,

如上所示,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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM