http://blog.csdn.net/h570768995/article/details/50386935
First to download LitJson.dll, placed in the plugins directory;
Litjson can be obtained from the following address: http://download.csdn.net/detail/h570768995/9373927
Then we define the JSON format, for example we need the following format:
{"Intvalue": 345, "Longvalue": 345679876, "StringValue": "Xiaoxian", "bytevalue": ' V ', "Doublevalue": 345.87}
In order to be able to JSON format, we need to define the following classes:
[CSharp]View PlainCopy
- Public class Messagejson
- {
- public int intvalue;
- public long Longvalue;
- public string stringvalue;
- public byte Bytevalue;
- public double doublevalue;
- }
Then declare the class in the method, and then map it to JSON format:
[CSharp]View PlainCopy
- Messagejson Msgjson = new Messagejson ();
- Msgjson.intvalue = 20;
- Msgjson.longvalue = 10000000000000L;
- Msgjson.stringvalue = "Chenhao";
- Msgjson.bytevalue = (byte) msgjson.intvalue;
- Msgjson.doublevalue = 3153456.125651;
- String jsondatapost = Jsonmapper.tojson (Msgjson);
So the data is sent out:
[CSharp]View PlainCopy
- www www = new www ("http://192.168.1.192:18080/test", Encoding.UTF8.GetBytes (Jsondatapost));
Then wait for the data and print it out:
[CSharp]View PlainCopy
- While (!www.isdone)
- {
- Debug.Log ("Wait");
- }
- Yield return www;
- if (www.error!=null)
- {
- Debug.logerror (Www.error);
- }
- Else
- {
- Debug.Log (Www.text);
- Fetching Data 1
- Messagejson Msgjsonrecieve = jsonmapper.toobject<messagejson> (Www.text);
- Debug.Log (Msgjsonrecieve.intvalue);
- Debug.Log (Msgjsonrecieve.longvalue);
- Debug.Log (Msgjsonrecieve.stringvalue);
- Debug.Log (Msgjsonrecieve.bytevalue);
- Debug.Log (Msgjsonrecieve.doublevalue);
- Fetching Data 2
- Jsondata jsondata = Jsonmapper.toobject (Www.text);
- if (jsondata["StringValue"]! = null)
- {
- Debug.Log (jsondata["StringValue"). ToString ());
- }
The jsonmapper mapping ignores sorting issues, and it only looks at keys in key-value pairs.
"Go" Unity uses WWW http to transmit JSON data