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.

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.

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.

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

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.

}