
Update: I’ve released version 1.1 of this code and posted it here. The new version formats the output, which should be much easier to read than the one-long-string that version 1.0 spits out.
After reading Dan Wood’s post here, I remembered something Buzz said a few weeks ago. He mentioned that since JSON is basically a series of key-value pairs, it would be a good match for NSDictionary - and it should be easy to create a dictionary out of a JSON string.
Well, after seeing Dan’s post, I figured I would give it a try. Here is the result. The main category is on NSDictionary and provides a method to create a new, autoreleased NSDictionary from an NSString containing an object in JSON format. The second method will output the contents of a dictionary as a string in JSON format. If you’re doing any work with web services that have JSON output (Yahoo, for example), this may be useful to you.
Some caveats:
- The output from -jsonStringValue isn’t formatted. It’s one long string. I thought of a few ways to make it look nicer and more human-readable, but wasn’t sure if developers would find it useful. Do you want something like that? Leave a comment.
- The output will not format numbers in the exponential format (for example, 2.32e5). Internally, it just uses the -stringValue method of NSDecimalNumber, which just outputs the decimal notation. It accepts input JSON using the exponential format just fine, but if you want e-notation output, leave a comment.
- When outputting strings, I don’t check to see if any of them are outside the ASCII range and then encode them in Unicode using the “\uXXXX” notation. I will probably add this in the next update.
- Neither the +dictionaryWithJSONString nor the -jsonStringValue method check for the “\/” escape sequence. This is technically part of JSON, but it’s not required. It sounds like it’s only required when sending HTML code. If this is a feature you need, please leave a comment.
I’ve run this code through a pretty thorough set of examples - including taking JSON, converting it to a dictionary, converting it back to JSON, converting it back to a dictionary, and comparing the dictionaries. I haven’t yet run it through a full suite of negative unit tests, though. I’m going to finish out the unit tests, but if you find any major bugs besides the caveats listed above, please let me know.
Download the code here.
Hey Blake,
Cool code! I’ve write some code to do the same thing late last year. You beat me putting it online though. http://toxicsoftware.com/blog/index.php/weblog/entry/cocoajson/
You can never have too much code! And I gotta give Buzz the credit - it was his idea and I think he started on some code, too. I’m eager to see how you did a few things in your code. (For example, I see that you skip white space by just scanning characters from the whitespaceAndNewline NSCharacterSet - there were several instances where this simply didn’t work for me. I tried everything I could think of - including making sure that the spaces I was testing with were truly space characters. I switched the code so it used a more manual method and it seems to be working great.
Yep, choice is good.
If you have any sample JSON dictionaries that you had trouble scanning whitespace with I’d be grateful to get a copy so I can add them to my test cases and/or fix any problems that come up.
Hey, I just stumbled upon this today. This is really an awesome little piece of code, and is working wonders for me. Before this I was hacking bad regexs to extract info from my json strings. You’re a huge help. I’ll be sure to give you credit once my project’s done.
One can never have too much good code! Thanks for this. I too had several instances where skipping white space by just scanning characters from the whitespaceAndNewline NSCharacterSet didn’t work well either. Manual worked best for me too. I’m thinking obviously it’d be better if the automated scan were more auto-intelligent.