Skip to content

Instantly share code, notes, and snippets.

@Edudjr
Last active April 4, 2016 23:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Edudjr/cb407c67e76ac36bcfac to your computer and use it in GitHub Desktop.
Save Edudjr/cb407c67e76ac36bcfac to your computer and use it in GitHub Desktop.
MiniJSON example using Classes
#pragma strict
import MiniJSON;
import System.Collections.Generic;
class JsonData { // UnityScript only; impossible in JavaScript
var root : Dictionary.<String,System.Object>; //root of string: root = {"data":"data",...}
var array : List.<System.Object>;
var object : Obj; //another json string
var string : String;
var unicode : String;
var _int : long; // ints come out as longs
var _float : double; // floats come out as doubles
var bool : boolean;
function JsonData(jsonString : String){
this.root = Json.Deserialize(jsonString) as Dictionary.<String,System.Object>;
this.array = this.root['array'] as List.<System.Object>;
this.string = this.root['string'] as String;
this.object = new Obj( Json.Serialize(this.root['object']) );
this.unicode = this.root['unicode'] as String;
this._int = this.root['int'];
this._float = this.root['float'];
this.bool = this.root['bool'];
}
class Obj {
var root : Dictionary.<String,System.Object>;
var key1 : String;
var key2 : String;
function Obj(jsonString : String){
this.root = Json.Deserialize(jsonString) as Dictionary.<String,System.Object>;
this.key1 = this.root['key1'] as String;
this.key2 = this.root['key2'] as String;
}
}
}
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 data = new JsonData(jsonString);
Debug.Log(data.array[0]);
Debug.Log(data.object.key1);
Debug.Log(data.string);
Debug.Log(data.unicode);
Debug.Log(data._int);
Debug.Log(data._float);
Debug.Log(data.bool);
}
@chrisdrogaris
Copy link

I get this error:

Assets/HSController.js(2,8): BCE0021: Namespace 'MiniJSON' not found, maybe you forgot to add an assembly reference?

@Edudjr
Copy link
Author

Edudjr commented Apr 29, 2015

Have you copied the MiniJSON file to the Plugins folder? It has to be compiled first!

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