- Home /
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.
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.
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.
NullReferenceException: Object reference not set to an instance of an object
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.
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?
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
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.
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613100608im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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