[Unity&JSON]如何在同一个JSON文档中存入多个不同类型的数据

时间:2024-03-17 10:37:24


JSON文本存储内容如下所示:

有两个 list , 一个是 equip_List ,另一个是item_List

equip_List 存放 的是 Equip_Attri 内部类的 equip_attri

item_List 存放的是 Item_Basic 内部类 的item_basic

[Unity&JSON]如何在同一个JSON文档中存入多个不同类型的数据


[Unity&JSON]如何在同一个JSON文档中存入多个不同类型的数据

想要实现如下所示的效果。

{
"equip_List":[
{"item_basic_id":"sixsixsix",
"item_basic_name":"666",
"test_array":["test11","test22"],
"equip_basic_attri_num":[0,1,0,1,0,1],
"equip_basic_attri_str":["durability","quality","weight","material","value","charm"],
"equip_resis_attri_num":[1,0,1,0,1,0],
"equip_resis_attri_str":["blow","virus","infection","resistance","bullet","fire"],
"equip_state_attri_num":[6,6,6,6],
"equip_state_attri_str":["comfort","clean","warm","wet"]}
,
{"item_basic_id":"sixsixsix","item_basic_name":"777","test_array":["test11","test22"],
"equip_basic_attri_num":[0,1,0,1,0,1],
"equip_basic_attri_str":["durability","quality","weight","material","value","charm"],
"equip_resis_attri_num":[1,0,1,0,1,0],
"equip_resis_attri_str":["blow","virus","infection","resistance","bullet","fire"],
"equip_state_attri_num":[7,7,7,7],
"equip_state_attri_str":["comfort","clean","warm","wet"]}
]
,
"item_List":
[
{"item_basic_id":"11","item_basic_name":"22","test_array":["head_eye","head_ear"]},
{"item_basic_id":"22","item_basic_name":"33","test_array":["hand_arm","hand_elbow"]}
]
}


[Unity&JSON]如何在同一个JSON文档中存入多个不同类型的数据

实际上 UNITY 保存起来,是没有那个 逗号,的。

然而,使用代码读取数据,却是出现了错误。


------------------------------------------------------------------------------------------------------------

那么究竟该如何 放入数据


[Unity&JSON]如何在同一个JSON文档中存入多个不同类型的数据


要想实现 多个 不同类型 存入同一个 JSON 文档的内容 ,就必须要使用如上图代码所示的, 新建一个 list 表,分别包含了这2个不同类型的类型 变量 即可实现。

[Unity&JSON]如何在同一个JSON文档中存入多个不同类型的数据

实现的数据存储的JSON文档代码:

{
"_itemlist":{"item_List":
[{"item_basic_id":"11",
"item_basic_name":"22",
"test_array":["head_eye","head_ear"]},
{"item_basic_id":"22",
"item_basic_name":"33",
"test_array":["hand_arm","hand_elbow"]}
]}
,
"_equiplist":{"equip_List":[{
"item_basic_id":"sixsixsix",
"item_basic_name":"666",
"test_array":["test11","test22"]
,"equip_basic_attri_num":[0,1,0,1,0,1],
"equip_basic_attri_str":["durability","quality","weight","material","value","charm"]
,"equip_resis_attri_num":[1,0,1,0,1,0],
"equip_resis_attri_str":["blow","virus","infection","resistance","bullet","fire"],
"equip_state_attri_num":[6,6,6,6],
"equip_state_attri_str":["comfort","clean","warm","wet"]},
{"item_basic_id":"sixsixsix",
"item_basic_name":"777",
"test_array":["test11","test22"],
"equip_basic_attri_num":[0,1,0,1,0,1],
"equip_basic_attri_str":["durability","quality","weight","material","value","charm"],
"equip_resis_attri_num":[1,0,1,0,1,0],
"equip_resis_attri_str":["blow","virus","infection","resistance","bullet","fire"],
"equip_state_attri_num":[7,7,7,7],
"equip_state_attri_str":["comfort","clean","warm","wet"]}
]}
}