- Home /
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.
Answer by Bunny83 · Aug 24, 2012 at 04:06 PM
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 :D
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.
Hey Bunny83,
I'm using your json parser and it's pretty rad! I found a bug (and it's fix), do you have a project on github or something similar? Let me know, I'd love to share. Find me on github @maxatbrs
No, i didn't have a github rep. I just realized that i haven't posted it on the wiki when i made this post. I changed already alot. I'll add the wiki link ;)
Since it's on the wiki, feel free to fix any bugs if you're sure that it is a bug. If you're not sure, just tell me what you've found. You can also add a note on the discussion page of the wiki page.
Hi Bunny83, how do I get a key and a collection of keys from a JSONNode?
@mGlushed: I'm not sure what you mean. Do you want to get a list of all keys stored in an JSONClass? I didn't implement that, but it should be no problem to expose the $$anonymous$$eys property of the internal Dictionary.
All i have implemented is the IEnumerable interface which allows you to iterate through all $$anonymous$$eyValuePairs in the dictionary.
You could add this property to the JSONClass class:
// C#
public Dictionary<string, JSONNode>.$$anonymous$$eyCollection $$anonymous$$eys
{
get { return m_Dict.$$anonymous$$eys; }
}
this should allow you to simply do this with a JSONNode which actually is a JSONClass:
// C#
JSONNode N;
//[...]
foreach(string key in N.AsObject.$$anonymous$$eys)
{
// Do something with key
}
// or create a List or array from the $$anonymous$$eyCollection.
// This requires Linq-Extension methods, so make sure you have imported Linq.
string[] keyArray = N.AsObject.$$anonymous$$eys.ToArray();
List< string >keyList = N.AsObject.$$anonymous$$eys.ToList();
I haven't tested it, but it should work ;)
Answer by podperson · Dec 29, 2012 at 09:31 PM
I've recently updated JSONParse and it should be pretty robust now. The new code is here:
https://github.com/tonioloewald/jsonparse
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.
Hi Podperson, I'm running your demo script , but I get this error:
Exception: json error -- not an array undefined json.error (.json json_obj, System.String msg) (at Assets/JSONParse.js:51) json._get (Int32 index) (at Assets/JSONParse.js:378)
I'm not very experienced with JSON so maybe I'm doing something wrong? Do you have an idea? Thanks
Turi
I'd need to see the json you're parsing to comment usefully.
Answer by sebas77 · Nov 14, 2013 at 04:16 PM
I have used successfully minijson https://gist.github.com/darktable/1411710
Answer by angelusiones · Sep 19, 2015 at 03:32 AM
The nicer lib for C# :
https://github.com/AngelQuirogaM/NiceJson
Very straightforward , input/output with strings , mantaining types.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080122im_/https://answers.unity.com/themes/thub/images/avi.jpg)