Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by xKroniK13x · Aug 24, 2012 at 03:22 PM · errorparse

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 17 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mwfelker · Feb 28, 2013 at 04:59 AM 0
Share

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

avatar image Bunny83 · Feb 28, 2013 at 08:32 AM 0
Share

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.

avatar image Bunny83 · Feb 28, 2013 at 08:34 AM 0
Share

btw, the LazyCreator is pretty cool ;)

avatar image Brockoala · Mar 12, 2013 at 10:13 PM 0
Share

Hi Bunny83, how do I get a key and a collection of keys from a JSONNode?

avatar image Bunny83 · Mar 13, 2013 at 12:13 AM 0
Share

@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 ;)

Show more comments
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image turi · Apr 16, 2013 at 10:14 AM 0
Share

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

avatar image podperson · Apr 12, 2014 at 09:17 PM 0
Share

I'd need to see the json you're parsing to comment usefully.

avatar image
0

Answer by sebas77 · Nov 14, 2013 at 04:16 PM

I have used successfully minijson https://gist.github.com/darktable/1411710

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# script parse error 1 Answer

WWW is not ready downloading yet? 1 Answer

How do I fix the YAML error? 1 Answer

error CS8032: Internal compiler error during parsing, Run with -v for details 1 Answer

I need help with the eval() function of javascrit 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges