LitJSON创建解析

    ///------------JSONToClass------------

    /// <summary>
    /// ListJSON参考
    /// </summary>
    void LitJSONFunction()
    {       
        /// JsonData JSON的基本数据类型
        /// JsonMapper JSON的类型转换器        
        string str = "{'name':'testName','id':10,'items':[{'itemid':1001,'itemname':'hello'},{'itemid':1002,'itemname':'hello2'}]}";
        /// JSON数据写入
        JsonData data = new JsonData();
        data["name"] = "peiandsky";
        data["age"] = 28;
        data["sex"] = "male";
        /// JSON数据转成串
        string json1 = data.ToJson();
        Debug.Log(json1);
        /// 使用JSONMapper解析JSONdata数据
        JsonData jsonData2 = JsonMapper.ToObject(str);
        Debug.Log(jsonData2["name"]);
        Debug.Log(jsonData2["id"]);
        Debug.Log(jsonData2["items"]);
        /// 使用JSONMapper将自定义转成串
        Player player = new Player();
        player.name = "peiandsky";
        player.age = 23;
        player.sex = "male";
        string json = JsonMapper.ToJson(player);
        /// 使用JSONMapper解析自定义格式
        Player player2 = JsonMapper.ToObject<Player>(json);    
    }

    public class Player
    {
        public string name = "name";
        public int age = 23;
        public string sex = "sex";
    }

   ///------------stringToJSON------------

    /// <summary>
    ///  StringTOJSON解析参考
    /// </summary>
    /// <param name="strJson"></param>
    void ReadJSON(string strJson)
    {
        string strReference = "{AndroidPath:{IsLeaves:{ Dir:AMap,IsLeaves:[{Dir:GridMapV3},{Dir:openamaplocationsdk},{Dir:data}]}}}";
        /// 传入值
        JsonData data0 = JsonMapper.ToObject(FileContext);
        Debug.Log("0Key: " + data0["AndroidPath"]);
        string value0 = data0["AndroidPath"].ToJson();
        Debug.Log("0Value: " + value0);

        JsonData data1 = JsonMapper.ToObject(value0);
        Debug.Log("1Key: " + data1["IsLeaves"]);
        string value1 = data1["IsLeaves"].ToJson();
        Debug.Log("1Value: " + value1);

        JsonData data2 = JsonMapper.ToObject(value1);
        Debug.Log("2Key<1>: " + data2["Dir"]);
        Debug.Log("2Key<2>: " + data2["IsLeaves"]);
        //Debug.Log("------------>>"+data2[2]);
        int array = data2["IsLeaves"].Count;
        //JsonData JsArrayIndex;      
        for(int i = 0; i < array; i++)
        {
            Debug.Log("索引" + i + "Key: " + data2["IsLeaves"][i]);
            Debug.Log("索引" + i + "Key: " + data2["IsLeaves"][i].ToJson());
            JsonData jsElementIndex = JsonMapper.ToObject(data2["IsLeaves"][i].ToJson());
            Debug.Log("索引" + i + "Key:------------------------->>> " + jsElementIndex["Dir"]);
            //Debug.Log("索引" + i + "Key: " + data1["Dir"]);          
            //strIndexValue = JsArrayIndex.ToJson();
            //JsElementIndex = JsonMapper.ToObject(strIndexValue);
            //Debug.Log("索引"+i+"Key: " + data1["Dir"]);      
        }
    }

           ///------------ListJSON数组的创建------------

             JsonData data11Array=new JsonData();

            JsonData data11Element1 = new JsonData();
            data11Element1["Dir"] = "10";
            JsonData data11Element2 = new JsonData();
            data11Element2["Dir"] = "17";
            JsonData data11Element3 = new JsonData();
            data11Element3["Dir"] = "15";
            /// JSON数组添加JSON对象
            data11Array.Add(data11Element1);
            data11Array.Add(data11Element2);
            data11Array.Add(data11Element3);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值