0

我是 iOS 和 JSON 解析的新手,我正在获取一些 JSON 数据,例如,

 [
     {
         "id":3,
         "name":"SCORM 0",
         "visible":1,
         "summary":"",
         "summaryformat":1,
         "modules":[
                    {
                        "id":1,
                        "url":"http:\/view.php?id=1",
                        "name":"Course01",
                        "visible":1,
                        "modicon":"http:\theme\/image.php\/standard\/scorm\/1378190687\/icon",
                        "modname":"scorm",
                        "modplural":"SCORM packages",
                        "indent":0
                    },
                    {
                        "id":2,
                        "url":"http:\/\/192.168.4.196\/moodle\/mod\/forum\/view.php?id=2",
                        "name":"News forum",
                        "visible":1,
                        "modicon":"http:\//image.php\/standard\/forum\/1378190687\/icon",
                        "modname":"settle",
                        "modplural":"Forums",
                        "indent":0
                    }
                    ]
     },
     {
         "id":2,
         "url":"http:\/\/view.php?id=2",
         "name":"News forum",
         "visible":1,
         "modicon":"http:\/\theme\/image.php\/standard\/forum\/1378190687\/icon",
         "modname":"forum",
         "modplural":"Forums",
         "indent":0
     }
     ]

我需要将数据分开"modname" != "forum"并将相应的数据存储在数组中。

帮助和解决方案将不胜感激。

4

2 回答 2

3
NSMutableArray *jsonArray = [[NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e] mutableCopy];

jsonArray = [jsonArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    return ![evaluatedObject[@"modname"] isEqualToString:@"forum"];
}];

这是你可以做什么的草图

于 2013-09-06T08:53:35.557 回答
1
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];

    NSLog(@"%@",json);

    NSLog(@"%@",delegate.firstArray);

    NSArray * responseArr = json[@"Deviceinfo"];

    for(NSDictionary * dict in responseArr)
    {
        [delegate.firstArray addObject:[dict valueForKey:@"id"]];
        [delegate.secondArray addObject:[dict valueForKey:@"name"]];
        [delegate.thirdArray addObject:[dict valueForKey:@"visible"]];
        [delegate.fourthArray addObject:[dict valueForKey:@"summery"]];

    }

这里所有数据都按照您的密钥排列

于 2013-09-06T09:14:12.597 回答