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;
}
}
}