Skip to content

Instantly share code, notes, and snippets.

@darktable
Created August 30, 2012 21:54
Show Gist options
  • Save darktable/3542254 to your computer and use it in GitHub Desktop.
Save darktable/3542254 to your computer and use it in GitHub Desktop.
Unity3D: Example of using MiniJSON in UnityScript (JavaScript).
#pragma strict
import MiniJSON;
import System.Collections.Generic;
function Start () {
var jsonString = "{ \"array\": [1.44,2,3], " +
"\"object\": {\"key1\":\"value1\", \"key2\":256}, " +
"\"string\": \"The quick brown fox \\\"jumps\\\" over the lazy dog \", " +
"\"unicode\": \"\\u3041 Men\\u00fa sesi\\u00f3n\", " +
"\"int\": 65536, " +
"\"float\": 3.1415926, " +
"\"bool\": true, " +
"\"null\": null }";
var dict = Json.Deserialize(jsonString) as Dictionary.<String,System.Object>;
Debug.Log("deserialized: " + dict.GetType());
Debug.Log("dict['array'][0]: " + ((dict["array"]) as List.<System.Object>)[0]);
Debug.Log("dict['string']: " + dict["string"] as String);
Debug.Log("dict['float']: " + dict["float"]); // floats come out as doubles
Debug.Log("dict['int']: " + dict["int"]); // ints come out as longs
Debug.Log("dict['unicode']: " + dict["unicode"] as String);
var str = Json.Serialize(dict);
Debug.Log("serialized: " + str);
}
@Edudjr
Copy link

Edudjr commented Sep 3, 2014

Thanks! I've made another example, but using classes. This way you can use your variable just like in Javascript.
https://gist.github.com/Edudjr/cb407c67e76ac36bcfac#file-classminijson-js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment