本文章由cartzhang編寫,
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 parser :http://json.parser.online.fr/ Json在線編輯:http://www.kjson.com/jsoneditor/?f=1
第二個是可以直接下載保存到本地的,此操作甚好??!
先看看,在Unity中,我們在其Json工具類中,可用的函數,總共就5個,好少啊!不過,simple is better.
#region 程序集 UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null// H:/Unity/PRoject/JsonReadTest/Library/UnityAssemblies/UnityEngine.dll#endregionusing System;namespace UnityEngine{ // // 摘要: // /// // Utility functions for working with JSON data. // /// public static class JsonUtility { // // 摘要: // /// // Create an object from its JSON representation. // /// // // 參數: // json: // The JSON representation of the object. // // type: // The type of object represented by the JSON. // // 返回結果: // /// // An instance of the object. // /// [WrapperlessIcall] public static object FromJson(string json, Type type); public static T FromJson<T>(string json); // // 摘要: // /// // Overwrite data in an object by reading from its JSON representation. // /// // // 參數: // json: // The JSON representation of the object. // // objectToOverwrite: // The object that should be overwritten. [WrapperlessIcall] public static void FromJsonOverwrite(string json, object objectToOverwrite); // // 摘要: // /// // Generate a JSON representation of the public fields of an object. // /// // // 參數: // obj: // The object to convert to JSON form. // // prettyPrint: // If true, format the output for readability. If false, format the output for minimum // size. Default is false. // // 返回結果: // /// // The object's data in JSON format. // /// public static string ToJson(object obj); // // 摘要: // /// // Generate a JSON representation of the public fields of an object. // /// // // 參數: // obj: // The object to convert to JSON form. // // prettyPrint: // If true, format the output for readability. If false, format the output for minimum // size. Default is false. // // 返回結果: // /// // The object's data in JSON format. // /// [WrapperlessIcall] public static string ToJson(object obj, bool prettyPrint); }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990可以看到,主要就是創建對象的和變成Json格式的兩個類型。
我們需要一個Json文件,這是主角啊,就是要讀取其內容啊。 好了。我隨便寫了個,命名為Test.json放到了我的./Assets/Resources/目錄下; Json文件內容如下:
{"gameName":"JSON Serializer Test","version":"1.0","isStereo":"false","isUseHardWare":"true","statusList":[{"name":"test","id":"1u702"}]}1
1首先,我們需要創建一個對象類,用來存放從Json文本中讀取的內容。 給這個類命名為GameStatus.cs
using UnityEngine;using System;using System.Collections;[Serializable]public class GameStatus{ public string gameName; public string version; public bool isStereo; public bool isUseHardWare; public refencenes[] statusList;}[Serializable]public class refencenes{ public string name; public int id;}12345678910111213141516171819202122
12345678910111213141516171819202122這個寫了一個靜態類,以后也可添加寫的內容 名字為LoadJson.cs
using UnityEngine;using System.Collections;using System.IO;using System.Runtime.Serialization.Formatters.Binary;public class LoadJson : MonoBehaviour{ public static GameStatus LoadJsonFromFile() { BinaryFormatter bf = new BinaryFormatter(); if (!File.Exists(application.dataPath + "/Resources/Test.json")) { return null; } StreamReader sr = new StreamReader(Application.dataPath + "/Resources/Test.json"); //FileStream file = File.Open(Application.dataPath + "/Test.json", FileMode.Open, Fileaccess.ReadWrite); //if (file.Length == 0) //{ // return null; //} //string json = (string)bf.Deserialize(file); //file.Close(); if (sr == null) { return null; } string json = sr.ReadToEnd(); if (json.Length > 0) { return JsonUtility.FromJson<GameStatus>(json); } return null; }}1234567891011121314151617181920212223242526272829303132333435363738394041424344
1234567891011121314151617181920212223242526272829303132333435363738394041424344說明:代碼被注釋掉的部分,是從網上找的,解析Json為二進制格式,然后在反序列化為字符串,結果,可能是編碼格式問題,不能正確的反序列化,總是報錯。 我表示無奈,只好從寫實使用了StreamReader來處理。
調用蠻簡單的,就是在Update中,使用L鍵來加載Json文件。 代碼如下:
using UnityEngine;using System.Collections;using System;using System.Runtime.Serialization.Formatters.Binary;using System.IO;public class ReadJson : MonoBehaviour{ void Update() { if(Input.GetKeyDown(KeyCode.S)) { //Save(); } if (Input.GetKeyDown(KeyCode.L)) { GameStatus status = LoadJson.LoadJsonFromFile(); Debug.Log(status); } }}12345678910111213141516171819202122232425
12345678910111213141516171819202122232425在場景中,建立一個空對象,然后把ReadJson拖拽到對象上,運行,按下L鍵,就可以使用斷點查看,當然也后Log輸出。
結果如下圖: 

總體過程都很簡單。工程就不一一上傳。
若有問題,請隨時聯系! 非常感謝! –張偉
新聞熱點
疑難解答