How to use NSJSONSerialization

后端 未结 12 584
天涯浪人
天涯浪人 2020-11-22 08:38

I have a JSON string (from PHP\'s json_encode() that looks like this:

[{\"id\": \"1\", \"name\":\"Aaa\"}, {\"id\": \"2\", \"name\":\"Bbb\"}]


        
相关标签:
12条回答
  • 2020-11-22 09:00

    Your code seems fine except the result is an NSArray, not an NSDictionary, here is an example:

    The first two lines just creates a data object with the JSON, the same as you would get reading it from the net.

    NSString *jsonString = @"[{\"id\": \"1\", \"name\":\"Aaa\"}, {\"id\": \"2\", \"name\":\"Bbb\"}]";
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *e;
    NSMutableArray *jsonList = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
    NSLog(@"jsonList: %@", jsonList);
    

    NSLog contents (a list of dictionaries):

    jsonList: (
               {
                   id = 1;
                   name = Aaa;
               },
               {
                   id = 2;
                   name = Bbb;
               }
               )
    
    0 讨论(0)
  • 2020-11-22 09:01

    This is my code for checking if the received json is an array or dictionary:

    NSError *jsonError = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonError];
    
    if ([jsonObject isKindOfClass:[NSArray class]]) {
        NSLog(@"its an array!");
        NSArray *jsonArray = (NSArray *)jsonObject;
        NSLog(@"jsonArray - %@",jsonArray);
    }
    else {
        NSLog(@"its probably a dictionary");
        NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
        NSLog(@"jsonDictionary - %@",jsonDictionary);
    }
    

    I have tried this for options:kNilOptions and NSJSONReadingMutableContainers and works correctly for both.

    Obviously, the actual code cannot be this way where I create the NSArray or NSDictionary pointer within the if-else block.

    0 讨论(0)
  • 2020-11-22 09:02

    Your root json object is not a dictionary but an array:

    [{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}]
    

    This might give you a clear picture of how to handle it:

    NSError *e = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];
    
    if (!jsonArray) {
      NSLog(@"Error parsing JSON: %@", e);
    } else {
       for(NSDictionary *item in jsonArray) {
          NSLog(@"Item: %@", item);
       }
    }
    
    0 讨论(0)
  • 2020-11-22 09:04

    @rckoenes already showed you how to correctly get your data from the JSON string.

    To the question you asked: EXC_BAD_ACCESS almost always comes when you try to access an object after it has been [auto-]released. This is not specific to JSON [de-]serialization but, rather, just has to do with you getting an object and then accessing it after it's been released. The fact that it came via JSON doesn't matter.

    There are many-many pages describing how to debug this -- you want to Google (or SO) obj-c zombie objects and, in particular, NSZombieEnabled, which will prove invaluable to you in helping determine the source of your zombie objects. ("Zombie" is what it's called when you release an object but keep a pointer to it and try to reference it later.)

    0 讨论(0)
  • 2020-11-22 09:08
    [{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}]
    

    In above JSON data, you are showing that we have an array contaning the number of dictionaries.

    You need to use this code for parsing it:

    NSError *e = nil;
    NSArray *JSONarray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];
            for(int i=0;i<[JSONarray count];i++)
            {
                NSLog(@"%@",[[JSONarray objectAtIndex:i]objectForKey:@"id"]);
                 NSLog(@"%@",[[JSONarray objectAtIndex:i]objectForKey:@"name"]);
            }
    

    For swift 3/3+

       //Pass The response data & get the Array
        let jsonData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [AnyObject]
        print(jsonData)
        // considering we are going to get array of dictionary from url
    
        for  item  in jsonData {
            let dictInfo = item as! [String:AnyObject]
            print(dictInfo["id"])
            print(dictInfo["name"])
        }
    
    0 讨论(0)
  • 2020-11-22 09:10

    It works for me. Your data object is probably nil and, as rckoenes noted, the root object should be a (mutable) array. See this code:

    NSString *jsonString = @"[{\"id\": \"1\", \"name\":\"Aaa\"}, {\"id\": \"2\", \"name\":\"Bbb\"}]";
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *e = nil;
    NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
    NSLog(@"%@", json);
    

    (I had to escape the quotes in the JSON string with backslashes.)

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