【Unity】使用LitJson保存和读取数据的例子

LitJson 是一个轻量级的 JSON 解析和生成库,广泛应用于 .NET 环境中。

优点:轻量级,易用,性能优秀,支持LINQ和自定义对象的序列化和反序列化。

public class LitJsonTest : MonoBehaviour

{

// Start is called before the first frame update

void Start()

{

PersonOfJson personOfJson1 = new();

personOfJson1.name = "keixo";

personOfJson1.age = 19;

//将数据转为字符串

string jsonStr1 = JsonMapper.ToJson(personOfJson1);

Debug.Log(jsonStr1);

//将字符串转为对象

PersonOfJson personOfJson4 = JsonMapper.ToObject<PersonOfJson>(jsonStr1);

//设有json数据

string jsonStr2 = @"{""name"" : ""zhaoxi"", ""age"" : 23 }";

PersonOfJson personOfJson5 = JsonMapper.ToObject<PersonOfJson>(jsonStr2);

Debug.Log(personOfJson5.name);

string jsonStr3 = @"

{

""name"" : ""alex"",

""id"" : 131231232,

""1"":[

{

""n1"" : ""n1"",

""n2"" : 1

},

{

""n1"" : ""n11"",

""n2"" : 2

}

]

}";

//可以把每个键值对都看成JsonData对象进行使用

JsonData jsonData = JsonMapper.ToObject(jsonStr3);

Debug.Log(jsonData["name"]);//alex

Debug.Log(jsonData["id"]);//131231232

JsonData jsonDate_Array = jsonData["1"];

//遍历数组

for(int i = 0; i < jsonDate_Array.Count; i ++)

{

Debug.Log("n1 =" + jsonDate_Array[i]["n1"]);

Debug.Log("n2 =" + jsonDate_Array[i]["n2"]);

}

}

}

Serializable

public class PersonOfJson

{

public string name;

public int age;

}

Serializable

public class PersonsOfJson

{

public List<PersonOfJson> personOfJsonList;

public int id;

}

相关推荐
大飞pkz1 小时前
【Unity】使用XML进行数据读存的简单例子
xml·unity·c#·游戏引擎·游戏开发·数据读写
大飞pkz3 小时前
【Unity】如何解决UI中的Button无法绑定带参数方法的问题
ui·unity·游戏引擎·游戏开发·开发记录·button绑定
半瓶啤酒一醉方休6 小时前
unity bug
unity·游戏引擎·bug
虾球xz10 小时前
游戏引擎学习第250天:# 清理DEBUG GUID
c++·学习·游戏引擎
仙魁XAN11 小时前
Flutter 学习之旅 之 flutter 有时候部分手机【TextField】无法唤起【输入法软键盘】的一些简单整理
flutter·unity·华为手机·textfield·键盘唤不起
米芝鱼13 小时前
Unity URPShader:实现和PS一样的色相/饱和度调整参数效果(修复)
游戏·unity·游戏引擎·图形渲染·opengl·着色器
虾球xz15 小时前
游戏引擎学习第249天:清理调试宏
c++·学习·游戏引擎
scott.cgi16 小时前
「Unity3D」TextMeshPro使用TMP_InputField实现,输入框高度自动扩展与收缩
unity·ugui·textmeshpro·tmp_inputfield·inputfield高度自动化·unity输入框高度扩展·textmeshpro高度扩展
大飞pkz1 天前
【Unity】MVC的简单分享以及一个在UI中使用的例子
unity·c#·mvc·框架·ui框架·商业级ui框架