使用轻型json库litjson。
首先下载litjson,将包放入unity工程中的Plugins文件夹之下。
引用一下就可以使用了。
LitJson主要的类:JsonData,JsonMapper;
主要的方法:JsonMapper.ToJson():将字典转换成json字符串;
JsonMapper.ToObject():将json字符串转换成JsonData;
代码示例:
//将字典转换成Json
Dictionary<string, string> DicRequestJson = new Dictionary<string, string>();
DicRequestJson.Add("requestCode", "10000");
DicRequestJson.Add("type", "1");
string Dictionary2Json = JsonMapper.ToJson(DicRequestJson);
//解析json
JsonData jsondata = JsonMapper.ToObject(jsonstr);
JsonData subjson = jsondata["data"];
for (int i = 0; i < subjson.Count; i++)
{
string PicURL = subjson[i]["PicURL"].ToString();
string FileURL = subjson[i]["FileURL"].ToString();
Debug.Log("图片URL" + PicURL);
Debug.Log("文件url" + FileURL );
}