c# Json的简单使用方式。

19 篇文章 0 订阅

Json 能够直接与类进行互转。在做数据通讯的时候,能够很方便的进行数据传递,然后解析出来使用。 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using UnityEngine.UI;
public class json_Try : MonoBehaviour {  
	void Start () {
        Item a = new Item();
        a.rc = 0;
        a.text = "你好啊";
        a.service = "cn.yunzhisheng.chat";
        a.code = "ANSWER";
       List<_General> t =new List<_General>();

        _General t1 = new _General();
        _General t2 = new _General();
        t1.type = "T";
        t1.text = "还没有啊你对我最好了:)";
        t1.style = "HIGH_QUALITY";
        t2.type = "T2";
        t2.text = "2还没有啊你对我最好了:)";
        t2.style = "2HIGH_QUALITY";
        t.Add(t1);
        t.Add(t2);
        a.general = t;
        a.history = "cn.yunzhisheng.chat";
        a.responseld = "7695d37ca63f41c5911deb7c9012d739";
        string p = JsonMapper.ToJson(a);
        print(p);
        JsonData d =JsonMapper.ToObject(p);
        print(d["general"].Count);
        print(d["general"][1]["text"].ToString());
    }

    public class Item
    {
          public int rc
        {
            get;
            set;
        }
        public string text
        {
            get;
            set;
        }
        public string service
        {
            get;
            set;
        }
        public string code
        {
            get;
            set;
        }
        public List<_General> general
        {
            get;
            set;
        }

        public string history
        {
            get;
            set;
        }
        public string responseld
        {
            get;
            set;
        }
    }
    public class _General
    {
        public string type
        {
            get;
            set;
        }
        public string text
        {
            get;
            set;
        }
        public string style
        {
            get;
            set;
        }
    }
}

2. Litjson  并不支持float 类型 查看 jsonData 发现  支持的类型如下。

       public JsonData(bool boolean);
        public JsonData(double number);
        public JsonData(int number);
        public JsonData(long number);
        public JsonData(object obj);
        public JsonData(string str);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值