I have an iOS app that needs to process a response from a web service. The response is a serialized JSON string containing a serialized JSON object, looking something like this:
"{ \"name\" : \"Bob\", \"age\" : 21 }"
Note that this response is a JSON string, not a JSON object. What I need to do is deserialize the string, so that I get this:
{ "name" : "Bob", "age" : 21 }
And then I can use +[NSJSONSerialization JSONObjectWithData:options:error:]
to deserialize that into an NSDictionary
.
But, how do I do that first step? That is, how to I "unescape" the string so that I have a serialized JSON object? +[NSJSONSerialization JSONObjectWithData:options:error:]
only works if the top-level object is an array or a dictionary; it doesn't work on strings.
I ended up writing my own JSON string parser, which I hope conforms to section 2.5 of RFC 4627. But I suspect I've overlooked some easy way to do this using NSJSONSerialization
or some other available method.
Convert the string to data:
If you have nested JSON, then just call
JSONObjectWithData
twice:One should first ask, why the server just don't include the JSON, as a sub structure.
But anyway. The string you got seems to be an escaped JSON. What that actually means, is totally up to the web service developer. I suspect, that just the double quotes and an escape itself have been escaped with an escape
\
. The resulting string is not "serialized" - JSON is already serialized - but encoded. In order to revert it back - you need to "unescape" or decode it again:A little C++ snippet shows how (I know you asked for Objective-C -- but this is just too easy):
Edit: the code should also work for UTF-16 and UTF-32 -- with any endianness - and if the encoder just mechanically did what I suspect, it should also work for escaped unicode characters, e.g. \u1234 etc.
Edit - no, it won't work for UTF-16 and UTF-32. The sample would have to be fixed for that (which would be easy). But please ensure you have UTF-8 - which is almost always the case.
Prints:
{ "name" : "Bob", "age" : 21 }
Just cut off the leading and trailing quotes and then replace all \"s with ":