Json 解析 之 Litjson

1.  使用Litjson方式

 "result": [
    {
      "name": "丹顶鹤",
      "ColorImagePath": "Image/D_1",
      "GrayImagePath": "Image/D_1"//D_0
    },
    {
      "name": "东方白鹳",
      "ColorImagePath": "Image/D_2",
      "GrayImagePath": "Image/D_2"
    }
]

 解析:

 

public void ReadJson(string jsonStr)
    {
        JsonData js = JsonMapper.ToObject(jsonStr);
        JsonData jsArray = js["result"];
        for (int i = 0; i < jsArray.Count; i++)
        {
            ItemData data = new ItemData();
            data.name = jsArray[i]["name"].ToString();
            data.ColorImagePath = jsArray[i]["ColorImagePath"].ToString();
            data.GrayImagePath = jsArray[i]["GrayImagePath"].ToString();
            itemDatas.Add(data);
        }
    }

 

 

2. 使用 Newtonsoft 方式

 

{
  "ID": [
    {
      "id": 1,
      "name": "销售经理"
    },
    {
      "id": 2,
      "name": "仓储经理"
    },
    {
      "id": 3,
      "name": "生产经理"
    },
    {
      "id": 4,
      "name": "采购经理"
    },
    {
      "id": 5,
      "name": "财务经理"
    }
  ],
  "Dialogue": [
    {
      "cameraID": 0,
      "data": [
        {
          "id": 1,
          "pos": 0,
          "data": "今天接到华晨童车商贸有限公司订购900辆豪华童车订单,需要和生产部确定生产计划和仓储部确认出货计划。",
          "pj": "销售订单",
          "mao": false,
          "fraction": 5,
          "bill": [
            {
              "key": "数量",
              "val": "900"
            },
            {
              "key": "含税单价",
              "val": "3680"
            }
           
          ]
        }
      ]
    },
    {
      "cameraID": 1,
      "data": [
        {
          "id": 1,
          "pos": 1,
          "data": "大家好,我们今天接到900辆豪华童车订单,需要4月10日出货,货够吗?",
          "pj": "",
          "mao": true,
          "fraction": 0,
          "bill": []
        },
        {
          "id": 2,
          "pos": 0,
          "data": "仓库还有100辆,再生产800辆就够了,生产有问题吗?",
          "pj": "",
          "mao": true,
          "fraction": 0,
          "bill": []
        },
        {
          "id": 3,
          "pos": 0,
          "data": "没问题!豪华型童车的凭据日产量为100辆,我马上填写生产排产计划表",
          "pj": "生产排产计划表",
          "mao": true,
          "fraction": 5,
          "bill": [
            {
              "key": "订单交货期",
              "val": "4月10日"
            },
            {
              "key": "订单数量",
              "val": "900"
            }
          ]
        }
      ]
    }
  ]
}

 解析

public class DialogueData
{
    public int cameraID;
    public List peopleDatas = new List();
    //public Dictionary dicData = new Dictionary();
}

[System.Serializable]
public class PeopleData
{
    public int id;
    public int pos;
    public string data;
    public string pjName;
    public bool mao;
    /// 
    /// 分数
    /// 
    public float fraction;

    public Dictionary billDic = new Dictionary();

    public bool SubFraction(int sub = 1)
    {
        if (fraction > 0)
        {
            fraction = fraction - sub;
            return true;
        }
        return false;
    }
}



 private Dictionary idDic = new Dictionary();
    private List dialogueDatas = new List();
 void InitData()
    {
        string json = File.ReadAllText(FilePath.StreamingAssetsPath + "Config.json");
        JObject jsonObj = JObject.Parse(json);
        JArray jlist = JArray.Parse(jsonObj["ID"].ToString());
        for (int i = 0; i < jlist.Count; i++)
        {
            idDic.Add(jlist[i]["id"].Value(), jlist[i]["name"].Value());
        }

        JArray jData = JArray.Parse(jsonObj["Dialogue"].ToString());
        for (int i = 0; i < jData.Count; i++)
        {
            DialogueData dialogueData = new DialogueData();
            dialogueData.cameraID = jData[i]["cameraID"].Value();
            JArray jd = JArray.Parse(jData[i]["data"].ToString());

            for (int j = 0; j < jd.Count; j++)
            {
                PeopleData peopleData = new PeopleData();
                peopleData.id = jd[j]["id"].Value();
                peopleData.pos = jd[j]["pos"].Value();
                peopleData.data = jd[j]["data"].Value();
                peopleData.pjName = jd[j]["pj"].Value();
                peopleData.mao = jd[j]["mao"].Value();
                peopleData.fraction = jd[j]["fraction"].Value();
                dialogueData.peopleDatas.Add(peopleData);
                JArray jdBill = JArray.Parse(jd[j]["bill"].ToString());
                for (int k = 0; k < jdBill.Count; k++)
                {
                    peopleData.billDic.Add(jdBill[k]["key"].Value(), jdBill[k]["val"].Value());
                }
            }
            dialogueDatas.Add(dialogueData);
        }
    }

 

你可能感兴趣的:(C#,Unity)