帮我从json feed中获取每个元素?请

时间:2011-07-16 09:50:37

标签: iphone ios

我有这个JSON数据:

 {
    "data":{
        "mat_149":{
            "id":"149",
            "title":"The closing of 40% profit within 9 month",
            "teaser":"profit within 9 months only which is equal to 52% annual profit",
            "body":" The auction was presented in a very high and commercial lands.\u000d\u000a",
            "files":{
                "911":{
                    "fid":"911",
                    "filename":"22.JPG",
                    "filepath":"http://mysite/files/22_0.JPG"
                }
            }
        },
        "mat_147":{
            "id":"147",
            "title":"Company launches the city ",
            "teaser":"demands for distinguished lands.",
            "body":" The area size is quare meters This is evident through projects and many other projects.\u000d\u000a\u000d\u000a",
            "files":{
                "906":{
                    "fid":"906",
                    "filename":"2D7z.jpg",
                    "filepath":"http://mysite/dlr/files/2D7Z.jpg"
                }
            }
        },
        "mat_link":"mysite.com/"
    }
}

我正在使用json-framework解析它:

NSString *response = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding] ;
SBJSON *parser = [[SBJSON alloc] init];  
NSDictionary *data = (NSDictionary *) [parser objectWithString:response error:nil];  
NSLog(@"Data : %@", [data valueForKey:@"data"] );

我正在获取数据:

NSLog(@"Data : %@", [data objectForKey:@"data"] );

我正在获取数据,但我应该怎样做才能获得'fid','filename','filepath'这样的'文件'项。如何从'数据'n'文件'中获取每个元素并存储进入一些NSStrings ........

有人可以指出我必须做什么吗?请

1 个答案:

答案 0 :(得分:0)

他们都是子词典不是他们, 只是尝试记录整个字典,所以去:

NSLog(@"%@", data);

然后你可以看到结构。

要获取所有其他数据,您需要创建一个数据模型来保存所有数据,它知道要调用哪些键来获取特定字符串。

或者你可以调用[data allKeys]; 通过它来迭代,获得你拥有模型对象的词典。

E.g:

//在某处您声明了这个

NSArray *keys = [data allKeys]
for (NSString *key in [data allKeys]) {
NSDictionary *oneObject = [dictionary objectForKey:key];
MyObjectModel *object = [[MyObjectModel alloc] init];
object.id = [oneObject objectForKey:@"id"];
object.title = [oneObject objectForKey:@"title"];
//etc

//then you create another dict for files
}

亚历