相同的json对象值

时间:2014-02-21 06:50:34

标签: ios objective-c json

"RestInfo": [
        {
            "id": "1",
            "store_id": "3",
            "staff_id": "1075",
            "default_rest": "0",
            "restaurant_name": "Imagine Restaurant",
            "restaurant_location": "Sydney",
},
 {
            "id": "3",
            "store_id": "3",
            "staff_id": "1085",
            "default_rest": "0",
            "restaurant_name": "MealsAustralia",
            "restaurant_location": "Nsw",
},



NSError *err;
    NSURLResponse *response;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

NSArray *array=[jsonArray objectForKey:@"RestInfo"];

for (int i=0; i<[array count]; i++) {
    NSLog(@"the restrunt==%@",[[array objectAtIndex:i]objectForKey:@"restaurant_location"]);

我只能得到一个json值,但我需要得到更多的值请帮助我,我对目标C很新。

2 个答案:

答案 0 :(得分:1)

您将获得从字典中获取值所需的字典数组:

    for (int i=0; i<[array count]; i++) {
        NSDictionary *dicObj = [array objectAtIndex:i];
        NSLog(@"%@", [dicObj objectForKey:@"id"]);
        NSLog(@"%@", [dicObj objectForKey:@"store_id"]);
        NSLog(@"%@", [dicObj objectForKey:@"staff_id"]);
        NSLog(@"%@", [dicObj objectForKey:@"default_rest"]);
        NSLog(@"%@", [dicObj objectForKey:@"restaurant_name"]);
        NSLog(@"%@", [dicObj objectForKey:@"restaurant_location"]);
     }

答案 1 :(得分:0)

试试这个: -

for (int i=0; i<[array count]; i++)

NSLog(@"the restrunt==%@",[array addObject:[[[jsonArray objectForKey:@"RestInfo"] objectAtIndex:i] objectForKey:@"restaurant_location"]];