How to parse JSON in Objective-C

后端 未结 2 1230
傲寒
傲寒 2021-01-16 17:19

i have this json string

{
    \"@uri\": \"http://localhost:8080/TunisairRESTful/resources/cities/\",
    \"city\": [
        {
            \"@uri\": \"http:/         


        
相关标签:
2条回答
  • 2021-01-16 17:25

    Michael is right. Here's a more detailed answer:

    initWithContentsOfFile takes a string containing a file path (e.g. "/users/mehdi/documents/myFile.txt"). You seem to be passing in your actual JSON string, which isn't a file path. As a result, initWithContentsOfFile is probably returning nil.

    Check this by asking:

    if (myJSON == nil) NSLog(@"myJSON variable == nil!");
    

    If it is nil, then your code is also setting json and citysList to nil.

    Try this:

    NSDictionary *json    = [responseString JSONValue];
    NSArray *citysList    =  [json objectForKey:@"city"];
    NSLog(@" number of element : %d", [citysList count]);
    
    0 讨论(0)
  • 2021-01-16 17:34

    You're doing an initWithContentsOfFile but passing in a string. Have you tried simply [responseString JSONValue]? Your code suggests that responseString has the PATH to the file you're trying to open, not your full response string itself.

    0 讨论(0)
提交回复
热议问题