Json 能够直接与类进行互转。在做数据通讯的时候,能够很方便的进行数据传递,然后解析出来使用。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using UnityEngine.UI;
public class json_Try : MonoBehaviour {
void Start () {
Item a = new Item();
a.rc = 0;
a.text = "你好啊";
a.service = "cn.yunzhisheng.chat";
a.code = "ANSWER";
List<_General> t =new List<_General>();
_General t1 = new _General();
_General t2 = new _General();
t1.type = "T";
t1.text = "还没有啊你对我最好了:)";
t1.style = "HIGH_QUALITY";
t2.type = "T2";
t2.text = "2还没有啊你对我最好了:)";
t2.style = "2HIGH_QUALITY";
t.Add(t1);
t.Add(t2);
a.general = t;
a.history = "cn.yunzhisheng.chat";
a.responseld = "7695d37ca63f41c5911deb7c9012d739";
string p = JsonMapper.ToJson(a);
print(p);
JsonData d =JsonMapper.ToObject(p);
print(d["general"].Count);
print(d["general"][1]["text"].ToString());
}
public class Item
{
public int rc
{
get;
set;
}
public string text
{
get;
set;
}
public string service
{
get;
set;
}
public string code
{
get;
set;
}
public List<_General> general
{
get;
set;
}
public string history
{
get;
set;
}
public string responseld
{
get;
set;
}
}
public class _General
{
public string type
{
get;
set;
}
public string text
{
get;
set;
}
public string style
{
get;
set;
}
}
}
2. Litjson 并不支持float 类型 查看 jsonData 发现 支持的类型如下。
public JsonData(bool boolean);
public JsonData(double number);
public JsonData(int number);
public JsonData(long number);
public JsonData(object obj);
public JsonData(string str);