IT评测·应用市场-qidao123.com

标题: u3d中JSON数据处置惩罚 [打印本页]

作者: 数据人与超自然意识    时间: 2024-12-31 00:45
标题: u3d中JSON数据处置惩罚
一.认识JSON

1.1 Json概述


1.2 JSON示例

压缩格式:
[{"id": "15", "name":"eter"}, {"id": "12"}]
解析格式:
[
    {
        "id": "15",
        "name": "eter"
    },
    {
        "id": "12"
    }
]

收集几个解析网站:
https://www.json.cn/jsononline/
https://www.jsonla.com/
1.3 json语法注意事项

1.当有多个数据对象时,最外层用[]包裹,表示是一个数组;
2.每一对{}表示一个独立的数据对象;
3.json对象内的数据,以键值对的情势存在;
4.json中字符串需要以“”包裹;
5.json中需要用逗号进行数据分割,且最后位置不需要逗号;
二.Unity内利用JSON

1.Assets下创建文件夹Plugins(定名固定,引擎约定用于插件的文件夹)
2.下载LitJson.dll,并将其拖拽至该文件夹内
3.脚本内引入定名空间:using LitJson;
接下来就可以调用Json相关API了
三.Json API演示

下面示例将演示:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;  //引入命名空间
  5. public class Player
  6. {
  7.     public int ID { get; set; } //自动实现的属性
  8.     public string name;
  9.     private int lev;
  10.     public Player() { } //用于Json转对象,不可或缺
  11.     public Player(int _id, string _name, int _lev)
  12.     {
  13.         ID = _id;
  14.         name = _name;
  15.         lev = _lev;
  16.     }
  17.     public string printInfo()
  18.     {
  19.         return string.Format("id:{0}, name:{1}, lev:{2}", ID, name, lev);
  20.     }
  21. }
  22. public class JSONDemo : MonoBehaviour
  23. {
  24.     // Start is called before the first frame update
  25.     void Start()
  26.     {
  27.         Player p1 = new Player(30, "Peter", 10);
  28.         Debug.Log(p1.printInfo());
  29.         //单对象转Json
  30.         string str1 = JsonMapper.ToJson(p1);
  31.         Debug.Log("-------------单对象转Json");
  32.         Debug.Log(str1);
  33.         //多对象转Json
  34.         List<Player> playerList1 = new List<Player>();
  35.         Player p2 = new Player(31, "Tom", 11);
  36.         playerList1.Add(p1);
  37.         playerList1.Add(p2);
  38.         string str2 = JsonMapper.ToJson(playerList1);
  39.         Debug.Log("-------------多对象转Json");
  40.         Debug.Log(str2);
  41.         //Json转单对象
  42.         Player p3 = JsonMapper.ToObject<Player>(str1);  //会调用默认构造函数
  43.         Debug.Log("-------------Json转单对象");
  44.         Debug.Log(p3.printInfo());
  45.         //Json转多对象(较为常用)
  46.         JsonData jsonData = JsonMapper.ToObject(str2);
  47.         List<Player> playerList2 = new List<Player>();
  48.         for (int i = 0; i < jsonData.Count; i++)
  49.         {
  50.             Player temp = JsonMapper.ToObject<Player>(jsonData[i].ToJson());
  51.             playerList2.Add(temp);
  52.         }
  53.         Debug.Log("-------------Json转多对象");
  54.         for (int i = 0; i < playerList2.Count; i++)
  55.         {
  56.             Debug.Log(playerList2[i].printInfo());
  57.         }
  58.         //构造JsonData对象获取Json
  59.         JsonData jd = new JsonData();
  60.         jd["id"] = "101";
  61.         jd["name"] = "James";
  62.         Debug.Log("-------------构造JsonData对象获取Json");
  63.         Debug.Log(jd.ToJson());
  64.     }
  65. }
复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4