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 withjavascript
on theirwebsite
?
Any help/suggestion would be very helpful to me.
The correct JSON should presumably look something like:
But, if you're stuck this the format provided in your question, you need to make the dictionary mutable with
NSJSONReadingMutableContainers
and then callNSJSONSerialization
again for each of thosearray
entries:// ----------------- json for localfile---------------------------
//------------- json for urlfile-----------------------------------
//------------ json for urlfile by asynchronous----------------------
//------------- json for urlfile by synchronous----------------------
You may always unescape the
jsonData
before deliver it toNSJSONSerialization
. Or you may use the string got to construct anotherjson object
to get thearray
.NSJSONSerialization
is doing right, the value in your example should be a string.As another answer has said, that value is a string.
You can get around it by turning that string into data, as it seems to be a valid json string and then parse that json data object back into an array which you can add to your dictionary as the value for the key.