Json串解析Unity(兩種解析方法)
阿新 • • 發佈:2019-02-19
朋友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 () {
}
}
執行結果如下: