最近接手一個項目,在項目中有用到Json的地方,根據(jù)網(wǎng)上找到的資料 客戶端配置文件優(yōu)化策略,綜合評定發(fā)現(xiàn)LitJson還不錯,于是找到Json下載的網(wǎng)站的網(wǎng)站下載對應(yīng)的Json,進入LitJson后盡然提示我 404! WTF??。?!我可是會科學(xué)上網(wǎng)的人,于是去GitHub下載LitJSON,發(fā)現(xiàn)最近幾天剛更新過,估計性能會更好,但是只有對應(yīng)的源碼,沒有DLL,也不要緊,我已經(jīng)幫你把源碼轉(zhuǎn)成DLL了,LitJSON對應(yīng)DLL下載
更新目錄
-
更新 2018.05.18 目前測試發(fā)現(xiàn)如果轉(zhuǎn)換類中的字典Key為int時會有報錯的情況
-
更新 2019.04.6 序列化有float值時會報錯
Max allowed object depth reached while trying to export from type System.Single,需要把float轉(zhuǎn)換成double -
更新 2019.12.12 更新版本 LitJSON 0.15.0
下面來簡單介紹下LitJson的使用方法,更多示例參考GitHub上的文檔
示例所示,JsonMapper.ToJson將制定數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為Json字符,JsonMapper.ToObject<T>用于將Json字符轉(zhuǎn)換為指定數(shù)據(jù)結(jié)構(gòu)


這個示例介紹的是活的JsonData也可以按照索引器的方式訪問指定的元素

image.png

最后一個示例就是 JsonReader 與 JsonWriter , 這兩種類型實際上是該庫的基礎(chǔ),并且JsonMapper類型建立在它們之上,所以開發(fā)人員可以將JsonReader和JsonWriter類視為LitJSON的低級編程接口。


完整Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using LitJson;
using Custom.Log;
using System.Text;
public class LitJsonExample : MonoBehaviour
{
void Start()
{
//PersonToJson();
//JsonToPerson();
//LoadAlbumData(json);
PrintJson(sample);
this.Log(new String('-', 100));
WriterJson();
}
#region 方案一
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime Birthday { get; set; }
}
public void PersonToJson()
{
Person HaiLan = new Person();
HaiLan.Name = "菜鳥海瀾";
HaiLan.Age = 51;
HaiLan.Birthday = new DateTime(2018, 05, 17);
string JsonStr = JsonMapper.ToJson(HaiLan);
this.Log(JsonStr);
}
public void JsonToPerson()
{
string json = @"
{
""Name"" : ""菜鳥海瀾"",
""Age"" : 2018,
""Birthday"" : ""05/17/2018 00:00:00""
}";
Person HaiLan = JsonMapper.ToObject<Person>(json);
this.Log("JsonToPerson Is Name:" + HaiLan.Name);
}
#endregion
#region 方案二
string json = @"
{
""album"" : {
""name"" : ""The Dark Side of the Moon"",
""artist"" : ""Pink Floyd"",
""year"" : 1973,
""tracks"" : [
""Speak To Me"",
""Breathe"",
""On The Run""
]
}
}
";
public void LoadAlbumData(string json_text)
{
Debug.Log("Reading data from the following JSON string: " + json_text);
JsonData data = JsonMapper.ToObject(json_text);
// 像哈希表一樣訪問字典
this.Log("Album's name: {0}", data["album"]["name"]);
// 存儲在JsonData實例中的標(biāo)量元素可以轉(zhuǎn)換為它們的自然類型
string artist = (string)data["album"]["artist"];
int year = (int)data["album"]["year"];
this.Log("Recorded by {0} in {1}", artist, year);
// 數(shù)組也像常規(guī)列表一樣被訪問
this.Log("First track: {0}", data["album"]["tracks"][0]);
}
#endregion
#region 方案三
string sample = @"{
""name"" : ""菜鳥海瀾"",
""age"" : 2018,
""awake"" : true,
""n"" : 2018.0517,
""note"" : [ ""life"", ""is"", ""but"", ""a"", ""dream"" ]
}";
public void PrintJson(string json)
{
JsonReader reader = new JsonReader(json);
this.Log("{0,14} {1,10} {2,16}", "Token", "Value", "Type");
this.Log(new String('-', 100));
// Read()方法返回false時,沒有其他內(nèi)容可讀
while (reader.Read())
{
string type = reader.Value != null ?
reader.Value.GetType().ToString() : "";
this.Log("{0,14} {1,10} {2,16}",
reader.Token, reader.Value, type);
}
}
public void WriterJson()
{
StringBuilder sb = new StringBuilder();
JsonWriter writer = new JsonWriter(sb);
writer.WriteArrayStart();
writer.Write(1);
writer.Write(2);
writer.Write(3);
writer.WriteObjectStart();
writer.WritePropertyName("color");
writer.Write("blue");
writer.WriteObjectEnd();
writer.WriteArrayEnd();
this.Log(sb.ToString());
}
#endregion
}