0

嗨,我有以下需要解析的 json,但是,我正在努力解析内部数组。我目前只打印每个内部数组,但我想打印每个标题并将标题添加到数组中。感谢您的任何帮助!

JSON

{"nodes":[{
    "node":{
        "nid":"1420857",
        "title":"Title 1",
        "votes":"182",
        "popular":"True",
        "teaser":"Teaser 1"
    }},
    {"node":{
        "nid":"1186152",
        "title":"Title 2",
        "votes":"140",
        "popular":"True",
        "teaser":"Teaser 2"
    }},
    {"node":{
        "nid":"299856",
        "title":"Title 3",
        "votes":"136",
        "popular":"True",
        "teaser":"Teaser 3"
    }}
]}

Json 解析器

    NSError *error = nil;
    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.somefilename.json"]];
    if (jsonData) {
        id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        if (error) {
            NSLog(@"error is %@", [error localizedDescription]);
            return;
        }
        NSArray *keys = [jsonObjects allKeys];
        for (NSString *key in keys) {
            NSLog(@"%@", [jsonObjects objectForKey:key]);
        }
    } else {
        // Handle Error
    }
4

2 回答 2

1

只需键入它:

NSArray *nodes = (NSArray*)[jsonObjects objectForKey:@"nodes"];
for (NSDictionary *node in nodes){
     // do stuff...
}

返回id(如-[objectForKey:], 和-[objectAtIndex:])的方法可以返回任何objective-c 对象。您需要提前知道将其类型转换为什么以对其执行适当的操作。JSON 被转换为 NSObject 等价物:

  • object->NSDictionary
  • array->NSArray
  • string->NSString
  • number->NSNumber
  • boolean->NSNumber
  • float->NSNumber
  • null->NSNull

要区分各种 NSNumber,您必须调用适当的类型方法:-[intValue]-[boolValue]-[floatValue]。查看NSNumber 文档了解更多信息。

于 2012-09-07T06:00:52.163 回答
0

你可以使用我的方法进行json解析,

解析方法:

    -(void)jsonDeserialize:(NSString *)key fromDict:(id)content completionHandler:(void (^) (id parsedData, NSDictionary *fromDict))completionHandler{
    if (key==nil && content ==nil) {
        completionHandler(nil,nil);
    }
    if ([content isKindOfClass:[NSArray class]]) {
        for (NSDictionary *obj in content) {
          [self jsonDeserialize:key fromDict:obj completionHandler:completionHandler];
        }
    }
    if ([content isKindOfClass:[NSDictionary class]]) {
        id result = [content objectForKey:key];
        if ([result isKindOfClass:[NSNull class]] || result == nil) {
            NSDictionary *temp = (NSDictionary *)content;
            NSArray *keys = [temp allKeys];
            for (NSString *ikey in keys) {
             [self jsonDeserialize:key fromDict:[content objectForKey:ikey] completionHandler:completionHandler];
            }
        }else{
            completionHandler(result,content);
        }
    }
}

方法调用:

 NSData *content = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Sample" ofType:@"json"]];
    NSError *error;

//获取序列化的json数据...

   id dictionary = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:&error];

//获取名为GetInfo的键的数据

     [self jsonDeserialize:@"GetInfo" fromDict:dictionary completionHandler:^(id parsedData, NSDictionary *fromDict) {
            NSLog(@"%@ - %@",parsedData,fromDict);
        }];
于 2013-11-29T03:55:39.997 回答