当前位置:   article > 正文

Unity--使用JsonUtility读取Json文件_jsonutility.fromjson

jsonutility.fromjson

1、JsonUtility读写Josn文件

        (1)读取Json文件

JsonUtility.FromJson<TestJson>(File.ReadAllText(JsonPath))

         JsonUtility.FromJson<Json结构类>(File.ReadAllText(Json文件地址))

       (2) 写入Josn文件

File.WriteAllText(JsonPath, JsonUtility.ToJson(TJ))

        JsonPath Json路径

        JsonUtility.ToJson(TJ)将修改后的Josn数据转为字符串

2、使用LitJson.dll文件读写Json文件(需要单独下载LitJson.dll文件)

        如果使用LitJson解析文件需要引用引用LitJson.dll

        (1)读取Json文件

JsonMapper.ToObject<TestJson>(File.ReadAllText(JsonPath));

        (2)写入Json文件

File.WriteAllText(JsonPath, JsonMapper.ToJson(TJ));

 使用 JsonUtility API读取Json文件

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. //using LitJson;
  6. public class ReadJson : MonoBehaviour
  7. {
  8. private string JsonPath=Application.streamingAssetsPath+ "/TestJson.Json";
  9. public static TestJson TJ=new TestJson ();
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. JsonRead();
  14. }
  15. // Update is called once per frame
  16. void Update()
  17. {
  18. if (Input.GetKeyDown(KeyCode.R))
  19. {
  20. Debug.Log("读取Json文件");
  21. JsonRead();
  22. }if (Input.GetKeyDown(KeyCode.W))
  23. {
  24. Debug.Log("写入json");
  25. JsonWrite();
  26. }
  27. }
  28. /// <summary>
  29. /// 读取Json文件
  30. /// </summary>
  31. public void JsonRead()
  32. {
  33. if (!File.Exists(JsonPath))//判断地址是否存在,如果不存在则提示并中直以下操作
  34. {
  35. Debug.LogError("地址不存在"+JsonPath);
  36. return;
  37. }
  38. TJ =JsonUtility.FromJson<TestJson>(File.ReadAllText(JsonPath));//使用File读取字符串内容,并使用JsonUtility读取到的文件解析为json数据
  39. // TJ = JsonMapper.ToObject<TestJson>(File.ReadAllText(JsonPath));//使用LitJson读取字符内容,并将读取到的文件解析为json数据
  40. Debug.Log("ID:"+TJ.ID);//打印读取对应的Id属性
  41. Debug.Log("Name:"+TJ.Name);//打印读取对应的Name属性
  42. Debug.Log("装备数量:"+TJ.Equips.Count);//打印读取对应的装备数量
  43. foreach (var item in TJ.Equips)//打印装备数组中的每个装备的数据
  44. {
  45. Debug.Log("装备名称:"+item.Name+"\n"+
  46. "装备类型:"+item.Label+"\n"+
  47. "装备伤害:"+item.Damage+"\n"+
  48. "装备防御:"+item.DefenseValue+"\n"+
  49. "装备描述:"+item.Describe);
  50. }
  51. }
  52. /// <summary>
  53. /// 写入json文件
  54. /// </summary>
  55. public void JsonWrite()
  56. {
  57. if (!File.Exists(JsonPath))//判断地址是否存在,如果不存在则提示并中直以下操作
  58. {
  59. Debug.LogError("地址不存在" + JsonPath);
  60. return;
  61. }
  62. TJ.Equips[1].Name = "双手剑";
  63. TJ.Equips[1].Label = "武器";
  64. TJ.Equips[1].Damage = "888";
  65. TJ.Equips[1].DefenseValue = "25";
  66. TJ.Equips[1].Describe = "这把双手剑的造型非常漂亮,从上面散发的光芒就可以感觉到此剑的非凡。";
  67. foreach (var item in TJ.Equips)//打印装备数组中的每个装备的数据
  68. {
  69. Debug.Log("装备名称:" + item.Name + "\n" +
  70. "装备类型:" + item.Label + "\n" +
  71. "装备伤害:" + item.Damage + "\n" +
  72. "装备防御:" + item.DefenseValue + "\n" +
  73. "装备描述:" + item.Describe);
  74. }
  75. File.WriteAllText(JsonPath, JsonUtility.ToJson(TJ));//使用JsonUtility写入Json内容
  76. //File.WriteAllText(JsonPath, JsonMapper.ToJson(TJ));//使用LitJson写入Json内容
  77. TJ = JsonUtility.FromJson<TestJson>(File.ReadAllText(JsonPath));//使用File读取Json内容,并将读取到的文件解析为json数据
  78. }
  79. }
  80. [Serializable]
  81. public class TestJson
  82. {
  83. public string ID;
  84. public string Name;
  85. public List<Equips> Equips;
  86. }
  87. [Serializable]
  88. public class Equips
  89. {
  90. /// <summary>
  91. /// 名称
  92. /// </summary>
  93. public string Name;
  94. /// <summary>
  95. /// 标签
  96. /// </summary>
  97. public string Label;
  98. /// <summary>
  99. /// 伤害
  100. /// </summary>
  101. public string Damage;
  102. /// <summary>
  103. /// 防御值
  104. /// </summary>
  105. public string DefenseValue;
  106. /// <summary>
  107. /// 装备描述
  108. /// </summary>
  109. public string Describe;
  110. }

 与之应Json文件

  1. {
  2. "ID": 6963,
  3. "Name": "测试用",
  4. "Equips": [
  5. {
  6. "Name": "头盔",
  7. "Label": "防具",
  8. "Damage": 12,
  9. "DefenseValue": 123,
  10. "Describe": "这是一个坚固的头盔"
  11. },
  12. {
  13. "Name": "大剑",
  14. "Label": "武器",
  15. "Damage": 666,
  16. "DefenseValue": 13,
  17. "Describe": "这把大剑锈上面有一些历史的痕迹"
  18. }
  19. ]
  20. }

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

闽ICP备14008679号