Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Parsing in iOS 7

Tags:

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

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

which they parse after escaping the \ character using Javascript.

My problem is when i parse it in iOS using the following command :

NSArray *result = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&localError]; 

and do this :

NSArray *Array = [result valueForKey:@"array"]; 

Instead of an Array I got NSMutableString object.

  • The website is already in production so I just cant ask them to change their existing structure to return a proper JSON object. It would be a lot of work for them.

  • So, until they change the underlying stucture, is there any way i can make it work in iOS like they do with javascript on their website?

Any help/suggestion would be very helpful to me.

like image 667
Ajeet Avatar asked Oct 16 '13 13:10

Ajeet


People also ask

What is JSON parsing in iOS?

JSON parsing in Swift is a common thing to do. Almost every app decodes JSON to show data in a visualized way. Parsing JSON is definitely one of the basics you should learn as an iOS developer. Decoding JSON in Swift is quite easy and does not require any external dependencies.

What is JSON data parsing?

JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON.

What is JSON parsing example?

JSON (JavaScript Object Notation) is a straightforward data exchange format to interchange the server's data, and it is a better alternative for XML. This is because JSON is a lightweight and structured language.

What is JSON iOS Swift?

JSON stands for JavaScript Object Notation. JSON is a lightweight format for storing and transporting data. The JSON format consists of keys and values. In Swift, think of this format as a dictionary where each key must be unique and the values can be strings, numbers, bools, or null (nothing).


1 Answers

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);     } } 
like image 173
Rob Avatar answered Nov 07 '22 08:11

Rob