LitJson解析例子

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;

public class LitJsonTest : MonoBehaviour {

// Use this for initialization
void Start () {
    Fun1();
}
void Fun1()
{

    JsonData heroJD = new JsonData();
    JsonData hero1JD = new JsonData();
    hero1JD["name"] = "xy";
    hero1JD["age"] = 9;
    JsonData hero2JD = new JsonData();
    hero2JD["name"] = "xj";
    hero2JD["age"] = 6;
    JsonData herosJD = new JsonData();
    herosJD.Add(hero1JD);
    herosJD.Add(hero2JD);

    heroJD["heros"] = herosJD;
    string str = heroJD.ToJson();
    Debug.Log(str);

    //string str="{'heros':[{'name':'xy','age':9},{'name':'xj','age':6}]}";
    JsonData xxJD = JsonMapper.ToObject(str);//0级,整个包括左右(键,值)
    JsonData xx1JD = xxJD["heros"];//1级,左边键
    foreach (JsonData xx2JD in xx1JD)
    {
        //Debug.Log(xx2JD["name"].ToString());//2级_1,(左边键的值=右边中第一个键)
        //Debug.Log((int)xx2JD["age"]);//2级_2,(左边键的值=右边中第二个键)
        string m = xx2JD["name"].ToString();
        int n = (int)xx2JD["age"];
        Debug.Log(m);
        Debug.Log(n);
    }
}

}

你可能感兴趣的:(LitJson解析例子)