朋友Json解析遇到点问题,我看了下他们服务器给他的Json串确实有点复杂,json串里套着json串,json里又有数组,数组里又套着数组:我耐心的帮他弄了一下,在Unity里解析如下(解析时需要用到LitJson.dll,f放到Plugins文件下)
sing UnityEngine;
using System.Collections;
using LitJson;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
getScore();
}
// Update is called once per frame
void Update () {
}
public void getScore()
{
//json串
string strJson = @"
{
""head"": {
""senddate"": ""20160406"",
""organcode"": ""231073"",
""tradecode"": ""20100"",
""workdate"": ""20160406"",
""devicecode"": ""PADLCZ"",
""reserve"": ""1"",
""sendtime"": ""170112"",
""version"": ""1.0""
},
""body"": {
""result"": ""0"",
""data"": [
{
""releasetime"": ""2017 -03-23 16:37:22"",
""status"": ""正常"",
""hottype"": ""最新产品"",
""medialist"": [
{
""filepath"": ""uploadFiles /uploadImgs/20161220/78c94cbd5f054f4d9570dc137332f4cd.png,uploadFiles/uploadImgs/20161219/9874efd2349a44c79caaef4b82ef02b0.png,uploadFiles/uploadImgs/20161219/bd3135171aae4fa5abad1d447f2a0198.png,uploadFiles/uploadImgs/20160723/ac53639ccaf4498b86fd51748dafb482.png"",
""newiphoneid"": """",
""productid"": ""484"",
""mediaid"": ""411"",
""filename"": ""tupian"",
""filetype"": ""IMAGE"",
""resolution"": """"
}
],
""productintroduction"": "" < p>XX科技PADLCZ</p>"",
""marketingdiscourse"": "" <p>XX科技PADLCZX</p>"",
""organcode"": ""231073"",
""productid"": ""484"",
""customerpad"": """",
""organname"": ""XX科技"",
""producttype"": ""理"",
""annualreturn"": ""11"",
""productname"": ""XX科技PADLCZ""
}
],
""msg"": ""获取产品信息成功""
}
}
";
JsonData jd = JsonMapper.ToObject(strJson);//获取Json串的数据
Debug.Log(jd.Count);
Debug.Log(jd["head"].ToJson());
JsonData jd1 = JsonMapper.ToObject(jd["head"].ToJson());//获取子json串数据
Debug.Log(jd1["senddate"].ToString());
Debug.Log(jd1["organcode"].ToString());
Debug.Log(jd1["tradecode"].ToString());
Debug.Log(jd1["workdate"].ToString());
Debug.Log(jd1["devicecode"].ToString());
JsonData jd2 = JsonMapper.ToObject(jd["body"].ToJson());//获取子json串数据
Debug.Log(jd2["result"].ToString());
JsonData jdItems = jd2["data"];//数组
for (int i = 0; i < jdItems.Count; i++) {
Debug.Log("releasetime = " + jdItems[i]["releasetime"]);
Debug.Log("status = " + jdItems[i]["status"]);
Debug.Log("hottype = " + jdItems[i]["hottype"]);
JsonData jdItems2 = jdItems[i]["medialist"];//数组里的数组
for (int j = 0; j < jdItems2.Count; j++)
{
Debug.Log("filepath = " + jdItems2[j]["filepath"]);
Debug.Log("filetype = " + jdItems2[j]["filetype"]);
}
Debug.Log("productintroduction = " + jdItems[i]["productintroduction"]);
Debug.Log("productname = " + jdItems[i]["productname"]);
}
Debug.Log(jd2["msg"].ToString());
}
}
最终测试解析成功
json 里{} 双括号表示对象,[] 中括号表示数组,"" 双引号内是属性或值,: 冒号表示后者是前者的值(这个值可以是字符串、数字、也可以是另一个数组或对象)
所以 {"name": "catl"} 可以理解为是一个包含name为cat的对象,而[{"name": "cat"},{"name": "dog"}]就表示包含两个对象的数组,当然了,也可以使用{"name":["cat","dog"]}来简化上面一部,这是一个拥有一个name数组的对象。
最近用unity自带的json解析工具重新解析了一下,代码如下,注意的一点是{ 前是类,[前是数组,定义的类里面的参数名字和json串里的名字得一致,否则会解析出错。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class AllData {
public Head head;
public Body body;
}
[System.Serializable]
public class Head {
public string senddate;
public string organcode;
public string tradecode;
public string workdate;
public string devicecode;
public string reserve;
public string sendtime;
public string version;
}
[System.Serializable]
public class Body {
public string result;
public datas[] data;
public string msg;
}
[System.Serializable]
public class datas
{
public string releasetime;
public string status;
public string hottype;
public Medialist[] medialist;
public string productintroduction;
public string marketingdiscourse;
public string organcode;
public string productid;
public string customerpad;
public string organname;
public string producttype;
public string annualreturn;
public string productname;
}
[System.Serializable]
public class Medialist
{
public string filepath;
public string newiphoneid;
public string productid;
public string mediaid;
public string filename;
public string filetype;
public string resolution;
}
public class Test000 : MonoBehaviour {
//json串
string strJson = @"
{
""head"": {
""senddate"": ""20160406"",
""organcode"": ""231073"",
""tradecode"": ""20100"",
""workdate"": ""20160406"",
""devicecode"": ""PADLCZ"",
""reserve"": ""1"",
""sendtime"": ""170112"",
""version"": ""1.0""
},
""body"": {
""result"": ""0"",
""data"": [
{
""releasetime"": ""2017 -03-23 16:37:22"",
""status"": ""正常"",
""hottype"": ""最新产品"",
""medialist"": [
{
""filepath"": ""uploadFiles /uploadImgs/20161220/78c94cbd5f054f4d9570dc137332f4cd.png,uploadFiles/uploadImgs/20161219/9874efd2349a44c79caaef4b82ef02b0.png,uploadFiles/uploadImgs/20161219/bd3135171aae4fa5abad1d447f2a0198.png,uploadFiles/uploadImgs/20160723/ac53639ccaf4498b86fd51748dafb482.png"",
""newiphoneid"": """",
""productid"": ""484"",
""mediaid"": ""411"",
""filename"": ""tupian"",
""filetype"": ""IMAGE"",
""resolution"": """"
}
],
""productintroduction"": "" < p>XX科技PADLCZ</p>"",
""marketingdiscourse"": "" <p>XX科技PADLCZX</p>"",
""organcode"": ""231073"",
""productid"": ""484"",
""customerpad"": """",
""organname"": ""XX科技"",
""producttype"": ""理"",
""annualreturn"": ""11"",
""productname"": ""XX科技PADLCZ""
}
],
""msg"": ""获取产品信息成功""
}
}
";
// Use this for initialization
void Start () {
AllData alldata = JsonUtility.FromJson<AllData>(strJson);
Debug.Log(alldata.head.senddate);
Debug.Log(alldata.head.devicecode);
Debug.Log(alldata.head.sendtime);
Debug.Log(alldata.body.result);
Debug.Log(alldata.body.data.Length);
Debug.Log(alldata.body.data[0].releasetime);
Debug.Log(alldata.body.data[0].medialist.Length);
Debug.Log(alldata.body.data[0].medialist[0].filename);
Debug.Log(alldata.body.data[0].organname);
Debug.Log(alldata.body.msg);
}
// Update is called once per frame
void Update () {
}
}
运行结果如下: