Best JSON Parser + Errors

Hey guys, I’m using this JSON Parser:
http://wiki.unity3d.com/index.php/JSONParse

Is this the best one available? Seems to be a bit glitchy… it gets hung up if there is a \ in the JSON, for example:
{ “name”: “\player” } would bring up an error :S

Any help would be great.
Also, if the JSON isn’t spaced out, like:
[ { “name”: “Test”, “desc”: “just a test” } , { “name”: “Test”, “desc”: “just a test” }

There will be a Nullreference error!
Weird!

Thanks guys.

Well, i’ve written a simple JSON parser (new link to the wiki page) in C# a while ago. I might post it on the wiki the other day, but there are already 4 other JSON parsers, so i’m not sure if it makes sense to “spam” the wiki with a lot similar stuff.

Here’s a sample in C# and here the same in UnityScript.

I wanted a typesafe parser, so i created utility classes for each JSON element. I treat all value types as string, but i have implemented “casting properties” for most types.
I also provide an IEnumerable interface, so they should work nicely with LINQ

I can’t guarantee that it won’t throw an exception especially by malformed JSON, but in general it should work. I use it for my UA crawler to parse the comments JSON :smiley:

It can also be used to create a JSON string from the DOM like structure.

edit Of course when you try to access something that doesn’t exist, it will also return “null”. So when it’s a JSON class / object, you can’t use an integer index like it would be an array. Same otherway round, when the node is an array you can’t access the members like they where class / object members.

You should know the structure when accessing the JSON. If not you should implement some null checks when you’re not sure if a certain node is available or not.

I’ve recently updated JSONParse and it should be pretty robust now. The new code is here:

It should be usable from C# (it’s written in UnityScript) and should allow you to both consume and generate valid JSON. Unlike the earlier implementation it does not rely on untyped variables.

I have used successfully minijson Unity3D: MiniJSON Decodes and encodes simple JSON strings. Not intended for use with massive JSON strings, probably < 32k preferred. Handy for parsing JSON from inside Unity3d. · GitHub

The nicer lib for C# :

Very straightforward , input/output with strings , mantaining types.