Unity3D研究院之Json序列化字典

综合技术 2017-04-16

Unity自己的json序列化是不支持字典格式的。无意间发现了一个全新的json .net库,功能很强大,还支持序列化字典推荐给大家。

http://www.newtonsoft.com/json

点击下载,zip. 解压后,把bin/net20/Newtonsoft.Json.dll 拖入unity工程。

写下一段简单的序列化 反序列化json的代码

usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
usingSystem;
usingNewtonsoft.Json;
 
public class Test : MonoBehaviour {
 
 void Start()
 {
 
 Productproduct = new Product();
 product.dic ["字典key"] = "字典Value";
 product.name="我是雨松MOMO";
 string json = JsonConvert.SerializeObject(product);
 
 Product m = JsonConvert.DeserializeObject(json);
 
 Debug.Log (json);
 Debug.Log (m.name);
 }
 
 
 public class Product 
 {
 public string name;
 public Dictionary dic=new Dictionary();
 }
 
}

用起来还是很方便的。支持复杂的嵌套,各种数据类型。大家可以去摸索。还有不用像unity那样每个序列话的对象还要写多一个[System.Serializable]标签。

这工具的缺点就是dll有点大。

欢迎大家一起测试~

雨松MOMO程序研究院

责编内容by:雨松MOMO程序研究院 (源链)。感谢您的支持!

您可能感兴趣的

Ultimate Guide to JSON Parsing with Swift 4 Swift 4 and Foundation has finally answered the question of how to parse...
New tricks for old MUDs: using JSON for flexible p... In the beginning (1990), the creators of Diku had many programming challenges...
Launch Json object to another class object in Andr... How to send a json object to android in codeigniter? I am ...
JSON Data Guide Enhancements in Oracle Database 18... This article covers the enhancements to the JSON Data Guide functionality in Ora...
How do I send Json to Struts2? This question already has an answer here: Send JSON object to Str...