JSON Parsing in iOS 7

前端 未结 14 2285
长情又很酷
长情又很酷 2020-12-04 11:25

I am creating an app for as existing website. They currently has the JSON in the following format :

[

   {
       \"id\": \"value\",
       \"array\": \"[{\         


        
14条回答
  •  醉话见心
    2020-12-04 12:23

    The correct JSON should presumably look something like:

    [
        {
            "id": "value",
            "array": [{"id": "value"},{"id": "value"}]
        },
        {
            "id": "value",
            "array": [{"id": "value"},{"id": "value"}]
        }
    ]
    

    But, if you're stuck this the format provided in your question, you need to make the dictionary mutable with NSJSONReadingMutableContainers and then call NSJSONSerialization again for each of those array entries:

    NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    if (error)
        NSLog(@"JSONObjectWithData error: %@", error);
    
    for (NSMutableDictionary *dictionary in array)
    {
        NSString *arrayString = dictionary[@"array"];
        if (arrayString)
        {
            NSData *data = [arrayString dataUsingEncoding:NSUTF8StringEncoding];
            NSError *error = nil;
            dictionary[@"array"] = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            if (error)
                NSLog(@"JSONObjectWithData for array error: %@", error);
        }
    }
    

提交回复
热议问题