开发者

Objective - C : How to read json? [duplicate]

开发者 https://www.devze.com 2023-04-13 08:22 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: How to parse JSON in Objective-C?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to parse JSON in Objective-C?

I totally new with Objective c, I need to value of distance from google distance matrix json, but data that I can get from json just the first element, so How to get value in distance this json?

{
"destination_addresses": [
    "San Francisco, Californie, États-Unis",
    "Victoria, BC, Canada"
],
"origin_addresses": [
    "Vancouver, BC, Canada",
    "Seattle, État de Washington, États-Unis"
],
"rows": [
    {
        "elements": [
            {
                "distance": {
                    "text": "1 732 km",
                    "value": 1732128
                },
                "duration": {
                    "text": "3 jours 23 heures",
                    "value": 340902
                },
                "status": "OK"
            }
        ]
    }
],
"status": "OK"
}

ok here is my sample code that just follow from JSON Tutorial :

I already use SBJSON , but I can only rows key, so how to get value in Distance key?

    SBJSON *parser = [[SBJSON 开发者_开发问答alloc] init];


NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL 

URLWithString:@"http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor=false"]];

// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];


NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

NSArray *statuses = [parser objectWithString:json_string error:nil];

for (NSDictionary *status in statuses)
{

  NSLog(@"%@ - %@", [status objectForKey:@"rows"]);
}


If you're targeting iOS 5 or OS X v10.7 then you can just use Apple's own NSJSONSerialization. You probably particularly want +JSONObjectWithData:options:error:. Using the built-in stuff is always preferable to third-party options because it eliminates the problem that the third party has no direct interest in fixing its bugs and no way to supply fixes for your application without your intervention.

If you want to support older devices then you probably want to import SBJSON — as as Rengers suggests — or any of the hundred others. For the reasons above I highly recommend you fall back on those only if NSJSONSerialization isn't available.

EDIT: it appears you already have SBJSON in place and are asking about how to traverse the results? From reading the JSON:

  • the root object will be a dictionary
  • within it, rows will be an array of dictionaries
  • each dictionary has an array named elements
  • each entry in that array is a dictionary
  • within that, distance is another dictionary
  • distance contains two strings, with keys "text" and "value"

So if you weren't to do any validation at all, to get to the distance dictionary you might do:

NSDictionary *result = [parser objectWithString:...];

NSLog(@"distance dictionary is: %@"
    [[[[[
        result objectForKey:@"rows"]
         objectAtIndex:0]
           objectForKey:@"elements"]
             objectAtIndex:0]
               objectForKey:@"distance"]
);

Based on your sample code, it looks like you may actually be getting an array of the sorts of dictionary posted rather than the dictionary directly. Obviously adapt if that's the case.


You need to use a JSON parser like SBJSON. This will create a NSDictionary/NSArray with all the values and keys. Then use the normal dictionary and array methods to access the data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号