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 Kr0n · Apr 19, 2013 at 12:13 AM · jsonparser

Json Parser

Im currently using the Boomlagoon JSON parser, but can't seem to retrieve object values:

string text = "{ \"sample\" : { \"sam\" : 23 } }"; ## string to parse

JSONObject json = JSONObject.Parse(text); ## parsed double number = json.GetNumber("sam"); ## Here i'm trying to jump into the sample object and retrieve the double value of sam which is 23. Debug.Log(number); Essentially I don't know how to jump into the sample object and retrieve the value associated with sam.

Any quick help is much appreciated.

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
2
Best Answer

Answer by Fattie · Apr 19, 2013 at 10:07 AM

I strongly recommend this one which is incredibly eays to use:

XML-JSON Serialization" from the Asset store ..

http://aworldforus.com/xmljson/

It is extremely easy to use,documented, continuously developed and they answer email instantly. Eg:

 var stream:JSONOutStream = new JSONOutStream( );
 stream
   .Content("name", "John Smith")
   .Content("height", 1.12)
   .End();
 Debug.Log("JSON done... " + stream.Serialize() );

couldn't be easier. works in both directions.

Comment
Add comment · Show 1 · 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 shaneparsons · Jun 02, 2016 at 02:00 PM 0
Share

Downside: it isn't free.

avatar image
2

Answer by SimonW · Jul 05, 2013 at 10:28 AM

Try this:

 double number = json.GetObject("sample").GetNumber("sam");

You have two nested objects, so you need to get the inner one as an object before you can get the value from it.

Comment
Add comment · Show 4 · 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 shaneparsons · Jun 02, 2016 at 02:21 PM 0
Share

NullReferenceException: Object reference not set to an instance of an object

avatar image SimonW shaneparsons · Jun 02, 2016 at 02:55 PM 0
Share

That suggests the first part of the call (GetObject) is failing, which means your data structure may not be what you were expecting (or you have a typo, or a number of other possible issues). You can get the first object into a local variable and add some checks to try and narrow down what's going wrong.

BTW, the code above (chaining get calls) assumes your JSON is co$$anonymous$$g from a source you control (e.g. loaded from a file). If you're getting it from the internet you'll need to be a lot more careful with checking and validation, there could be anything in there.

avatar image shaneparsons SimonW · Jun 02, 2016 at 03:00 PM 0
Share

Thanks for the quick reply.

The JSON I'm getting is from an api that I control. Here's the problematic json:

 {
     "id": 1,
     "username": "shaneparsons",
     "data": "{\"coins\":\"\",\"completed_stages\":\"\",\"decorations\":\"\",\"fish\":\"\",\"language\":\"english\",\"music\":\"1\",\"sfx\":\"1\",\"tank_size\":\"125\"}",
     "created_at": null,
     "updated_at": null
 }

I'm wondering if it's because only the nested "data" object has escaped quotes?

Show more comments
avatar image
0

Answer by Voronoi · Apr 26, 2014 at 06:04 PM

Here is how I did this with BoomLagoon:

 JSONObject jsonObject;
 
 JSONArray j = jsonObject.GetArray("sample");
 
 if( j.Length > 0)
 {
  double num = j[0].Obj.GetNumber("sam");
 }

Hope that helps

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 rakkarage · Jun 02, 2016 at 02:27 PM

https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html

 using UnityEngine;
 
 [Serializable]
 public class PlayerInfo
 {
     public string name;
     public int lives;
     public float health;
 
     public static PlayerInfo CreateFromJSON(string jsonString)
     {
         return JsonUtility.FromJson<PlayerInfo>(jsonString);
     }
 
     // Given JSON input:
     // {"name":"Dr Charles","lives":3,"health":0.8}
     // this example will return a PlayerInfo object with
     // name == "Dr Charles", lives == 3, and health == 0.8f.
 
 }

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

16 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

Related Questions

minijson pars 0 Answers

parse json 2 Answers

Using JSON to get tree data into JavaScript objects 3 Answers

JSon parser for iMac -1 Answers

Json . NET, iOS and AOT 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