在iPhone上使用json-framework需要帮助

时间:2010-07-18 06:51:13

标签: iphone ios json parsing

所以我已经在我的项目上运行了json-framework,但需要帮助找出如何使用它来解析这个json字符串:

[
    {
    "id":"0",
    "name":"name",
    "info":"This is info",
    "tags":
        [ 
            {
            "id":"36", 
            "tag":"test tag"
            },
            { 
            "id":"37",
            "tag":" tag 2"
            } 
        ],
    "other":"nil"
    },
    { 
    "id":"1",
    "name":"name",
    "info":"This is info",
    "tags":
        [ 
            { 
            "id":"36",
            "tag":"test tag" 
            },
            {
            "id":"37",
            "tag":" tag 2"
            } 
        ], 
    "other":"nil"
} 

任何有关如何使用这种特定类型的json的帮助和示例代码都会很棒。不知何故,我无法将其写入我能读出来的字典中。非常感谢。

1 个答案:

答案 0 :(得分:7)

您无法将此字符串输入字典的原因是它不是字典,它是字典数组

您可以通过将值存储在NSArray中来将值放入Objective-C对象中:

NSArray *objects = (NSArray*) [jsonString JSONValue];

然后,您可以循环遍历数组中的那些对象:

for(NSDictionary *dict in objects) {
    NSString *id = (NSString *) [dict objectForKey:@"id"];
    NSString *name = (NSString *) [dict objectForKey:@"name"];
    NSArray *tags = (NSArray *) [dict objectForKey: @"tags"];
    //loop over tags here...
    for(NSDictionary *tag in tags) {
        NSString *tag_id = (NSString *) [tag objectForKey:@"id"];
        NSString *tag_name = (NSString *) [tag objectForKey:@"tag"];
    }
    //...
}