How to parse json object in unity (2D) javascript?

I am following this link http://wiki.unity3d.com/index.php/JSONUtils but not able to parse json object.

what I have tried so far:

function SerializeObject()
{
 var object = {"response":[{"id":"100","name":"Guest","score":"14","game":"3,6,9,7,1,8,2,4","date":"2015-02-28 11:22:32"},{"id":"99","name":"Guest","score":"18","game":"7,8,2,5,6,9,4,3","date":"2015-02-28 11:19:35"},{"id":"89","name":"Guest","score":"17","game":"5,7,2,8,6,1,3,9","date":"2015-02-26 16:39:59"},{"id":"96","name":"Guest","score":"18","game":"2,6,1,5,9,7,8,4","date":"2015-02-26 16:34:05"},{"id":"97","name":"Guest","score":"16","game":"1,7,3,4,8,2,6,5","date":"2015-02-26 16:32:30"},{"id":"95","name":"Guest","score":"14","game":"1,3,7,4,6,9,2,8","date":"2015-02-23 19:20:07"},{"id":"90","name":"Guest","score":"16","game":"8,3,9,6,4,5,2,7","date":"2015-02-23 16:48:55"},{"id":"92","name":"Guest","score":"17","game":"5,2,1,9,7,4,3,8","date":"2015-02-23 16:48:28"},{"id":"91","name":"Guest","score":"16","game":"2,1,3,9,6,7,8,4","date":"2015-02-23 16:48:06"},{"id":"94","name":"Guest","score":"14","game":"5,2,8,7,6,1,9,4","date":"2015-02-23 16:47:25"},{"id":"93","name":"Guest","score":"16","game":"8,9,2,7,4,6,3,1","date":"2015-02-23 16:45:44"},{"id":"88","name":"jacky chain","score":"15","game":"1,2,5,4,7,9,3,6","date":"2015-02-23 13:35:20"},{"id":"87","name":"Genie","score":"15","game":"8,9,5,7,1,4,2,3","date":"2015-02-22 16:19:32"},{"id":"86","name":"Genie","score":"15","game":"9,7,3,2,1,5,8,4","date":"2015-02-22 16:16:13"},{"id":"85","name":"Genie","score":"15","game":"7,1,4,6,5,3,9,8","date":"2015-02-22 14:25:39"},{"id":"83","name":"new","score":"18","game":"2,5,4,1,8,9,7,6","date":"2015-02-22 11:11:49"},{"id":"84","name":"Guest","score":"15","game":"9,8,3,5,1,4,2,7","date":"2015-02-22 09:48:28"},{"id":"80","name":"Guest","score":"16","game":"5,4,2,3,1,8,7,6","date":"2015-02-22 08:24:55"},{"id":"82","name":"Guestr","score":"15","game":"8,1,9,5,7,4,6,3","date":"2015-02-21 21:00:37"},{"id":"81","name":"Guest","score":"18","game":"9,4,2,7,6,5,1,8","date":"2015-02-21 20:54:51"},{"id":"79","name":"Guest","score":"15","game":"2,6,9,5,8,3,1,7","date":"2015-02-21 20:37:30"},{"id":"78","name":"Guest","score":"15","game":"3,6,9,7,1,5,4,2","date":"2015-02-21 20:35:27"},{"id":"77","name":"Guest","score":"17","game":"5,3,7,9,8,4,1,6","date":"2015-02-21 16:04:17"},{"id":"64","name":"Guest","score":"17","game":"7,9,8,4,6,1,3,5","date":"2015-02-21 16:03:41"},{"id":"76","name":"new","score":"18","game":"9,3,4,8,6,5,2,1","date":"2015-02-21 15:27:25"}]};

  for( var test in object.Keys )
    {
     Debug.Log( "Object["+test+"] : "+object[test] );
    }
}

I see the below line in the Log:

Object[response] : Boo.Lang.Hash[]

Now i want to extract value from this object.

I would appreciate your help on this, thank you.

you need a class who handles json, i use this one :

http://wiki.unity3d.com/index.php/SimpleJSON

var N = JSON.Parse(object);
var id = N["response"][0]["id"];       

You can download the entire package from here : http://wiki.unity3d.com/images/8/82/SimpleJSON.zip

That’s not “parsing”, parsing a JSON means you have a string in JSON format (“{key1:value1,key2:value2,…,keyN:valueN}”) and want to create an object in memory with the same structure.

What you’re trying to do is iterate over all the values of the JSON object, right?

In that case you need some recursive function. You can think of a JSON objects as a list of key-value pairs, and the value of any par can be also a list of key-value pairs.

With your “for” loop your only getting the elemtns of the first list, the “root”, and that list has only one pair with key “response” and the rest as the value. You’ll need another for loop to iterate over the value.

I’m not sure about syntax of that plugin and I don’t code in UnityScript, so I don’t have code, but the idea should be:

  • Create a function that takes an object as a parameter and returns the serialized object
  • Inside de function, Get the keys of that object
  • Iterate over the keys
    • If the value is a single value, add the key and the value to the string you’ll return.
    • If the value for that key is a list, call the function again with that list as a parameter and use the returned string as the “value” when adding the key to the string.
  • After the loop ends, return the string you built.

Anyway, it looks like that JSONUtils alread have a method to serialize a JSON (JSONUtils.ObjectToJSON(object);), why are you implementing one?