当前位置:   article > 正文

Unity的Json解析<一>--读取Json文件

public static string tojson (object obj, bool prettyprint);

本文章由cartzhang编写,转载请注明出处。 所有权利保留。
文章链接:http://blog.csdn.net/cartzhang/article/details/50373558
作者:cartzhang

Unity的Json解析<一>–读取Json文件

因为需要做一个外部文件配置,考虑了XML和Json,而5.3版本对Json做了更新,所以就尝试一下。
版本更新的Json部分介绍哦: [Unity5.3版本更新的Json部分 ]

https://github.com/cartzhang/UnityJsonTest/blob/master/Assets/JSONSerialization.html

https://unity3d.com/cn/unity/whats-new/unity-5.3

https://blogs.unity3d.com/cn/2015/12/08/unity-5-3-all-new-features-and-more-platforms/

Json的在线编辑

Json parser :http://json.parser.online.fr/
Json在线编辑:http://www.kjson.com/jsoneditor/?f=1

第二个是可以直接下载保存到本地的,此操作甚好啊!

JsonUtility

先看看,在Unity中,我们在其Json工具类中,可用的函数,总共就5个,好少啊!不过,simple is better.

  1. #region 程序集 UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
  2. // H:\Unity\Project\JsonReadTest\Library\UnityAssemblies\UnityEngine.dll
  3. #endregion
  4. using System;
  5. namespace UnityEngine
  6. {
  7. //
  8. // 摘要:
  9. // ///
  10. // Utility functions for working with JSON data.
  11. // ///
  12. public static class JsonUtility
  13. {
  14. //
  15. // 摘要:
  16. // ///
  17. // Create an object from its JSON representation.
  18. // ///
  19. //
  20. // 参数:
  21. // json:
  22. // The JSON representation of the object.
  23. //
  24. // type:
  25. // The type of object represented by the JSON.
  26. //
  27. // 返回结果:
  28. // ///
  29. // An instance of the object.
  30. // ///
  31. [WrapperlessIcall]
  32. public static object FromJson(string json, Type type);
  33. public static T FromJson<T>(string json);
  34. //
  35. // 摘要:
  36. // ///
  37. // Overwrite data in an object by reading from its JSON representation.
  38. // ///
  39. //
  40. // 参数:
  41. // json:
  42. // The JSON representation of the object.
  43. //
  44. // objectToOverwrite:
  45. // The object that should be overwritten.
  46. [WrapperlessIcall]
  47. public static void FromJsonOverwrite(string json, object objectToOverwrite);
  48. //
  49. // 摘要:
  50. // ///
  51. // Generate a JSON representation of the public fields of an object.
  52. // ///
  53. //
  54. // 参数:
  55. // obj:
  56. // The object to convert to JSON form.
  57. //
  58. // prettyPrint:
  59. // If true, format the output for readability. If false, format the output for minimum
  60. // size. Default is false.
  61. //
  62. // 返回结果:
  63. // ///
  64. // The object's data in JSON format.
  65. // ///
  66. public static string ToJson(object obj);
  67. //
  68. // 摘要:
  69. // ///
  70. // Generate a JSON representation of the public fields of an object.
  71. // ///
  72. //
  73. // 参数:
  74. // obj:
  75. // The object to convert to JSON form.
  76. //
  77. // prettyPrint:
  78. // If true, format the output for readability. If false, format the output for minimum
  79. // size. Default is false.
  80. //
  81. // 返回结果:
  82. // ///
  83. // The object's data in JSON format.
  84. // ///
  85. [WrapperlessIcall]
  86. public static string ToJson(object obj, bool prettyPrint);
  87. }
  88. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

可以看到,主要就是创建对象的和变成Json格式的两个类型。

准备工作

我们需要一个Json文件,这是主角啊,就是要读取其内容啊。
好了。我随便写了个,命名为Test.json放到了我的.\Assets\Resources\目录下;
Json文件内容如下:

{"gameName":"JSON Serializer Test","version":"1.0","isStereo":"false","isUseHardWare":"true","statusList":[{"name":"test","id":"1u702"}]}
  • 1

创建对象类

首先,我们需要创建一个对象类,用来存放从Json文本中读取的内容。
给这个类命名为GameStatus.cs

  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. [Serializable]
  5. public class GameStatus
  6. {
  7. public string gameName;
  8. public string version;
  9. public bool isStereo;
  10. public bool isUseHardWare;
  11. public refencenes[] statusList;
  12. }
  13. [Serializable]
  14. public class refencenes
  15. {
  16. public string name;
  17. public int id;
  18. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

需要一个读取类

这个写了一个静态类,以后也可添加写的内容
名字为LoadJson.cs

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. public class LoadJson : MonoBehaviour
  6. {
  7. public static GameStatus LoadJsonFromFile()
  8. {
  9. BinaryFormatter bf = new BinaryFormatter();
  10. if (!File.Exists(Application.dataPath + "/Resources/Test.json"))
  11. {
  12. return null;
  13. }
  14. StreamReader sr = new StreamReader(Application.dataPath + "/Resources/Test.json");
  15. //FileStream file = File.Open(Application.dataPath + "/Test.json", FileMode.Open, FileAccess.ReadWrite);
  16. //if (file.Length == 0)
  17. //{
  18. // return null;
  19. //}
  20. //string json = (string)bf.Deserialize(file);
  21. //file.Close();
  22. if (sr == null)
  23. {
  24. return null;
  25. }
  26. string json = sr.ReadToEnd();
  27. if (json.Length > 0)
  28. {
  29. return JsonUtility.FromJson<GameStatus>(json);
  30. }
  31. return null;
  32. }
  33. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

说明:代码被注释掉的部分,是从网上找的,解析Json为二进制格式,然后在反序列化为字符串,结果,可能是编码格式问题,不能正确的反序列化,总是报错。
我表示无奈,只好从写实使用了StreamReader来处理。

调用

调用蛮简单的,就是在Update中,使用L键来加载Json文件。
代码如下:

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.IO;
  6. public class ReadJson : MonoBehaviour
  7. {
  8. void Update()
  9. {
  10. if(Input.GetKeyDown(KeyCode.S))
  11. {
  12. //Save();
  13. }
  14. if (Input.GetKeyDown(KeyCode.L))
  15. {
  16. GameStatus status = LoadJson.LoadJsonFromFile();
  17. Debug.Log(status);
  18. }
  19. }
  20. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

测试结果

在场景中,建立一个空对象,然后把ReadJson拖拽到对象上,运行,按下L键,就可以使用断点查看,当然也后Log输出。

结果如下图:
Unity视图

Debug

说明

总体过程都很简单。工程就不一一上传。


若有问题,请随时联系!
非常感谢!

转载于:https://www.cnblogs.com/qitian1/p/6461933.html

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

闽ICP备14008679号