最近接手一個(gè)項(xiàng)目,在項(xiàng)目中有用到Json的地方,根據(jù)網(wǎng)上找到的資料 客戶端配置文件優(yōu)化策略,綜合評(píng)定發(fā)現(xiàn)LitJson還不錯(cuò)勾徽,于是找到Json下載的網(wǎng)站的網(wǎng)站下載對(duì)應(yīng)的Json祠锣,進(jìn)入LitJson后盡然提示我 404! WTF继找?!L友亍婴渡!我可是會(huì)科學(xué)上網(wǎng)的人,于是去GitHub下載LitJSON,發(fā)現(xiàn)最近幾天剛更新過(guò)感挥,估計(jì)性能會(huì)更好缩搅,但是只有對(duì)應(yīng)的源碼,沒(méi)有DLL触幼,也不要緊硼瓣,我已經(jīng)幫你把源碼轉(zhuǎn)成DLL了,LitJSON對(duì)應(yīng)DLL下載
更新目錄
-
更新 2018.05.18 目前測(cè)試發(fā)現(xiàn)如果轉(zhuǎn)換類(lèi)中的字典Key為int時(shí)會(huì)有報(bào)錯(cuò)的情況
-
更新 2019.04.6 序列化有float值時(shí)會(huì)報(bào)錯(cuò)
Max allowed object depth reached while trying to export from type System.Single
置谦,需要把float轉(zhuǎn)換成double -
更新 2019.12.12 更新版本 LitJSON 0.15.0
下面來(lái)簡(jiǎn)單介紹下LitJson的使用方法堂鲤,更多示例參考GitHub上的文檔
示例所示,JsonMapper.ToJson將制定數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為Json字符媒峡,JsonMapper.ToObject<T>用于將Json字符轉(zhuǎn)換為指定數(shù)據(jù)結(jié)構(gòu)
這個(gè)示例介紹的是活的JsonData也可以按照索引器的方式訪問(wèn)指定的元素
最后一個(gè)示例就是 JsonReader 與 JsonWriter 瘟栖, 這兩種類(lèi)型實(shí)際上是該庫(kù)的基礎(chǔ),并且JsonMapper類(lèi)型建立在它們之上谅阿,所以開(kāi)發(fā)人員可以將JsonReader和JsonWriter類(lèi)視為L(zhǎng)itJSON的低級(jí)編程接口半哟。
完整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 = "菜鳥(niǎo)海瀾";
HaiLan.Age = 51;
HaiLan.Birthday = new DateTime(2018, 05, 17);
string JsonStr = JsonMapper.ToJson(HaiLan);
this.Log(JsonStr);
}
public void JsonToPerson()
{
string json = @"
{
""Name"" : ""菜鳥(niǎo)海瀾"",
""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);
// 像哈希表一樣訪問(wèn)字典
this.Log("Album's name: {0}", data["album"]["name"]);
// 存儲(chǔ)在JsonData實(shí)例中的標(biāo)量元素可以轉(zhuǎn)換為它們的自然類(lèi)型
string artist = (string)data["album"]["artist"];
int year = (int)data["album"]["year"];
this.Log("Recorded by {0} in {1}", artist, year);
// 數(shù)組也像常規(guī)列表一樣被訪問(wèn)
this.Log("First track: {0}", data["album"]["tracks"][0]);
}
#endregion
#region 方案三
string sample = @"{
""name"" : ""菜鳥(niǎo)海瀾"",
""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時(shí),沒(méi)有其他內(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
}