当前位置:   article > 正文

Unity 基础 之 Litjson 简单进行 json 创建与解析_lightjson 如何在unity中使用

lightjson 如何在unity中使用

Unity 基础 之 Litjson 简单进行json 创建与解析

 

目录

Unity 基础 之 Litjson 简单进行json 创建与解析

一、简单介绍

二、实现原理

三、注意事项

四、效果预览

五、实现步骤

六、关键代码

七、后记(在Unity中读取Resource 中的json数据,一次解析json 数据列表)


 

 

一、简单介绍

Unity中的一些基础知识点。

Unity游戏开发中,进行数据封装是很常见的操作,这里简单介绍使用 litjson ,进行json数据的创建和解析。

 

二、实现原理

1、JsonMapper.ToJson将制定数据结构转换为Json字符,进行 Json 数据创建

2、JsonMapper.ToObject<T>用于将Json字符转换为指定数据结构,进行 Json 数据解析

 

三、注意事项

1、请记得导入 Litjson.dll 插件

2、注意解析的使用 结构要保有 无参构造,不然 JsonMapper.ToObject<T> 可能会报错哈

 

四、效果预览

 

五、实现步骤

1、打开Unity,新建一个空工程,导入 Litjson.dll,如下图

 

2、新建脚本,使用Litjson ,进行数据的Json 创建与解析,如下图

 

3、把脚本挂载到场景中,如下图

 

4、运行场景,控制台 Console 打印如下

 

六、关键代码

1、JsonTest

  1. using LitJson;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class Hero
  6. {
  7. public string name;
  8. public int level;
  9. public int attack;
  10. // 这个要有,不然JsonMapper.ToObject<T>(jsonStr) 会报错
  11. public Hero(){}
  12. public Hero(string name, int level, int attack)
  13. {
  14. this.name = name;
  15. this.level = level;
  16. this.attack = attack;
  17. }
  18. }
  19. public class Enemy
  20. {
  21. public string name;
  22. public int attack;
  23. public string[] devices;
  24. // 这个要有,不然JsonMapper.ToObject<T>(jsonStr) 会报错
  25. public Enemy() { }
  26. public Enemy(string name, int attack, string[] devices)
  27. {
  28. this.name = name;
  29. this.attack = attack;
  30. this.devices = devices;
  31. }
  32. }
  33. public class Data
  34. {
  35. public string version;
  36. public Hero[] heros;
  37. public Enemy[] enemys;
  38. // 这个要有,不然JsonMapper.ToObject<T>(jsonStr) 会报错
  39. public Data(){}
  40. public Data(string version, Hero[] heros, Enemy[] enemys)
  41. {
  42. this.version = version;
  43. this.heros = heros;
  44. this.enemys = enemys;
  45. }
  46. }
  47. public class JsonTest : MonoBehaviour
  48. {
  49. Data data;
  50. // Start is called before the first frame update
  51. void Start()
  52. {
  53. Init();
  54. string jsonStr = DataObjectToJson(data);
  55. Debug.Log("DataObjectToJson :" + jsonStr);
  56. Data jsonToObject = DataJsonToObject<Data>(jsonStr);
  57. Debug.Log("DataJsonToObject:");
  58. Debug.Log("version:" + jsonToObject.version);
  59. Debug.Log("heros:");
  60. foreach(Hero hero in jsonToObject.heros){
  61. Debug.Log(hero.name + " " + hero.level + " " +hero.attack);
  62. }
  63. Debug.Log("enemys:");
  64. foreach (Enemy enemy in jsonToObject.enemys)
  65. {
  66. Debug.Log(enemy.name + " " + enemy.attack);
  67. foreach (string str in enemy.devices) {
  68. Debug.Log(str);
  69. }
  70. }
  71. }
  72. // Update is called once per frame
  73. void Update()
  74. {
  75. }
  76. void Init() {
  77. Hero superman = new Hero("SuperMan",20,345);
  78. Hero batman = new Hero("BatMan",19,321);
  79. Hero spiderman = new Hero("SpiderMan",19,301);
  80. Enemy joker = new Enemy("Joker",298,new string[] { "gun","knife"});
  81. Enemy blackWidow = new Enemy("BlackWidow", 301, new string[] { "gun", "knife" });
  82. data = new Data("1.0.2", new Hero[] {superman,batman,spiderman}, new Enemy[] {joker,blackWidow });
  83. }
  84. /// <summary>
  85. /// 转为json数据
  86. /// </summary>
  87. /// <typeparam name="T"></typeparam>
  88. /// <param name="t"></param>
  89. /// <returns></returns>
  90. string DataObjectToJson<T>(T t) {
  91. return JsonMapper.ToJson(t);
  92. }
  93. /// <summary>
  94. /// json 转为 object
  95. /// </summary>
  96. /// <typeparam name="T"></typeparam>
  97. /// <param name="jsonStr"></param>
  98. /// <returns></returns>
  99. T DataJsonToObject<T>(string jsonStr) {
  100. return JsonMapper.ToObject<T>(jsonStr);
  101. }
  102. }

 

 

七、后记(在Unity中读取Resource 中的json数据,一次解析json 数据列表)

JsonMapper.ToObject<List<Field>>(jsonString)

1、代码函数

  1. public class JsonParse:MonoBehaviour
  2. {
  3. List<Field> LocalArrowBleList = new List<Field>();
  4. IEnumerator GetLocalBowBeacons()
  5. {
  6. yield return null;
  7. TextAsset t = Resources.Load("BowBeacon") as TextAsset;
  8. string s = t.text;
  9. LocalArrowBleList = JsonMapper.ToObject<List<Field>>(s);
  10. StreamWriter sw = new StreamWriter(Application.dataPath + "arrowbeacons_0310_02.json", false, Encoding.UTF8);
  11. if((s != null))
  12. {
  13. Debug.Log(s);
  14. sw.WriteLine(s);
  15. }
  16. sw.Close();
  17. Debug.Log(LocalArrowBleList.Count.ToString());
  18. }
  19. }
  20. /// <summary>
  21. /// json数据结构
  22. /// </summary>
  23. public class Field
  24. {
  25. public string mac;
  26. public string device_id;
  27. public bool device_is_using;
  28. public string comment;
  29. public Field() { }
  30. public Field(string mac, string id, bool state, string comment) {
  31. this.mac = mac;
  32. this.device_id = id;
  33. this.device_is_using = state;
  34. this.comment = comment;
  35. }
  36. }

 

2、json 数据 (文件名:BowBeacon.txt)

  1. [{"mac":"FE:38:78:B7:3C:D4","device_id":"PFBOW00001","device_is_using":true,"comment":"北京市使用"},
  2. {"mac":"FE:38:78:B7:3C:FA","device_id":"PFBOW00002","device_is_using":true,"comment":"北京市使用"},
  3. {"mac":"FE:38:78:B7:3C:D8","device_id":"PFBOW00003","device_is_using":true,"comment":"北京市使用"},
  4. {"mac":"FE:38:78:B7:3C:E5","device_id":"PFBOW00004","device_is_using":true,"comment":"北京市使用"},
  5. {"mac":"00:00:00:70:00:23","device_id":"PFBOW00088","device_is_using":true,"comment":"深圳市使用"},
  6. {"mac":"00:00:00:70:00:2E","device_id":"PFBOW00081","device_is_using":true,"comment":"深圳市使用"},
  7. {"mac":"00:00:00:70:00:30","device_id":"PFBOW00069","device_is_using":true,"comment":"深圳市使用"},
  8. {"mac":"FE:38:78:B7:3D:91","device_id":"PFBOW00010","device_is_using":true,"comment":"深圳市使用"},
  9. {"mac":"FE:38:78:B7:3C:D0","device_id":"PFBOW001HY","device_is_using":true,"comment":"武汉市火油使用"},
  10. {"mac":"FE:38:78:00:01:02","device_id":"PFBOW002HY","device_is_using":true,"comment":"武汉市火油使用"}]

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/123445
推荐阅读
相关标签
  

闽ICP备14008679号