1 读取字符串数组案例
数组 链表 均可读取 中括号数组 [{},{}]
using UnityEngine;
using LitJson;
using System.Collections.Generic;
public class J_Test : MonoBehaviour
{
string json_str = @"
{
""album"":
[
{
""name"":""donger"",
""artist"":""Pink"",
""year"":""1973""
},
{
""name"":""donger1"",
""artist"":""Pink1"",
""year"":""1973_1""
}
]
}
";
public class Albums
{
public List<Album> album;
}
public class Album
{
public string name;
public string artist;
public string year;
}
void Start()
{
Albums alb = JsonMapper.ToObject<Albums>(json_str);
print(alb.album[0].artist);
print(alb.album[0].name);
print(alb.album[0].year);
print(alb.album[1].artist);
print(alb.album[1].name);
print(alb.album[1].year);
}
}
2 读取文件案例
json字段未加入 “” 则认为是int型 转换为string会报错
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
public class J_Test2 : MonoBehaviour
{
string Path_Json = Application.streamingAssetsPath + "/T2.json";
void Start()
{
ReadJson();
}
void ReadJson()
{
StreamReader tmpReader = File.OpenText(Path_Json);
string result = tmpReader.ReadToEnd();
JsonData json = JsonMapper.ToObject(result);
print((int)json["code"]);
print((string)json["message"]);
JsonData data = json["data"];
for (int i = 0; i < data.Count; i++)
{
Debug.Log("第" + (i + 1) + " 信息是 " + (string)data[i]["userName"]);
Debug.Log("第" + (i + 1) + " 信息是 " + (string)data[i]["remark"]);
Debug.Log("第" + (i + 1) + " 信息是 " + (int)data[i]["userNameLevel"]);
JsonData data1 = data[i]["userDeviceList"];
for (int j = 0; j < data1.Count; j++)
{
Debug.Log("第" + (j + 1) + " 信息是 " + (int)data1[j]["groupType"]);
Debug.Log("第" + (j + 1) + " 信息是 " + (string)data1[j]["groupName"]);
}
}
}
void WriteJson()
{
string names = "json - web";
using (StreamWriter sw = new StreamWriter(Path_Json))
{
sw.WriteLine(names);
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
print(1);
WriteJson();
}
}
}
3 json文件 需要放在流文件夹,win平台下可读写 Assets/StreamingAssets
{
"code": 0,
"message": "",
"data": [
{
"userName": "12301233210",
"remark": "0",
"userNameLevel": 4,
"userDeviceList": [
{
"groupType": 16,
"groupName": "222228",
"groupEquipmentgList": [
{
"hostCode": "200000000055",
"realHostCode": null,
"hostName": "办公室55号设备",
"markerCode": null,
"markerName": "undefined",
"isOnLine": null,
"labelTime": null,
"addTime": null,
"attributeInfo": {
"equipmentOtherJsonData": null,
"equipmentParametersJsonData": null,
"equipmentWarnLimitJsonData": null,
"monitorBaseJsonData": {
"deviceId": "200000000055",
"storageName": null,
"dateTime": 1639696920000,
"online": 1,
"state": null,
"coolerState": null,
"alarmInfo": null,
"ramarks": {
"temp1Alias": "温度1",
"temp1State": "1",
"temp1Synchro": "0",
"temp2Alias": "温度2",
"temp2State": "1",
"temp2Synchro": "0",
"temp3Alias": "温度3",
"temp3State": "0",
"temp3Synchro": "0",
"temp4Alias": "温度4",
"temp4State": "0",
"temp4Synchro": "0",
"temp5Alias": "温度5",
"temp5State": "1",
"temp5Synchro": "0",
"temp6Alias": "湿度",
"temp6State": "1",
"temp6Synchro": "0",
"temp7Alias": "二氧化碳浓度",
"temp7State": "1",
"temp7Synchro": "0",
"temp8Alias": "氨气浓度",
"temp8State": "1",
"temp8Synchro": "0",
"temp9Alias": "温度6",
"temp9State": "0",
"temp9Synchro": "0",
"temp10Alias": "湿度2",
"temp10State": "0",
"temp10Synchro": "0",
"temp11Alias": "二氧化碳浓度2",
"temp11State": "0",
"temp11Synchro": "0",
"temp12Alias": "氨气浓度2",
"temp12State": "0",
"temp12Synchro": "0",
"temp13Alias": "H2S浓度1",
"temp13State": "0",
"temp13Synchro": "0",
"temp14Alias": "预留14",
"temp14State": "1",
"temp14Synchro": "0",
"temp15Alias": "预留15",
"temp15State": "0",
"temp15Synchro": "0",
"temp16Alias": "预留16",
"temp16State": "0",
"temp16Synchro": "0",
"DI0Alias": "DI1",
"DI0State": "0",
"DI0Synchro": "0",
"DI1Alias": "DI2",
"DI1State": "0",
"DI1Synchro": "0",
"DI2Alias": "DI3",
"DI2State": "0",
"DI2Synchro": "0",
"DI3Alias": "DI4",
"DI3State": "0",
"DI3Synchro": "0",
"DI4Alias": "DI5",
"DI4State": "0",
"DI4Synchro": "0",
"DI5Alias": "DI6",
"DI5State": "0",
"DI5Synchro": "0",
"DI6Alias": "DI7",
"DI6State": "0",
"DI6Synchro": "0",
"DI7Alias": "DI8",
"DI7State": "0",
"DI7Synchro": "0",
"DO0Alias": "DO1",
"DO0State": "0",
"DO0Synchro": "0",
"DO1Alias": "DO2",
"DO1State": "0",
"DO1Synchro": "0",
"DO2Alias": "DO3",
"DO2State": "0",
"DO2Synchro": "0",
"DO3Alias": "DO4",
"DO3State": "0",
"DO3Synchro": "0",
"DO4Alias": "DO5",
"DO4State": "0",
"DO4Synchro": "0",
"DO5Alias": "DO6",
"DO5State": "0",
"DO5Synchro": "0",
"DO6Alias": "DO7",
"DO6State": "0",
"DO6Synchro": "0",
"DO7Alias": "DO8",
"DO7State": "0",
"DO7Synchro": "0",
"AlState": "0",
"AlSynchro": "0",
"AyState": "0",
"AySynchro": "0",
"BlState": "0",
"BlSynchro": "0",
"ByyState": "0",
"ByySynchro": "0",
"ClState": "0",
"ClSynchro": "0",
"CyState": "0",
"CySynchro": "0",
"DhState": "0",
"DhSynchro": "0",
"ShState": "0",
"ShSynchro": "0"
},
"temp1": "9.3",
"temp2": "9.6",
"temp3": "",
"temp4": "",
"temp5": "10.4",
"temp6": "15.5",
"temp7": "452",
"temp8": "1",
"temp9": "",
"temp10": "",
"temp11": "",
"temp12": "",
"temp13": "",
"temp14": "",
"temp15": "",
"temp16": "",
"dI0": "0",
"dI1": "0",
"dI2": "0",
"dI3": "0",
"dI4": "0",
"dI5": "",
"dI6": "",
"dI7": "",
"dO0": "0",
"dO1": "0",
"dO2": "0",
"dO3": "1",
"dO4": "0",
"dO5": "0",
"dO6": "0",
"dO7": "0",
"temp1Alarm": "0",
"temp2Alarm": "0",
"temp3Alarm": "0",
"temp4Alarm": "0",
"temp5Alarm": "0",
"temp6Alarm": "0",
"temp7Alarm": "0",
"temp8Alarm": "0",
"temp9Alarm": "0",
"temp10Alarm": "0",
"temp11Alarm": "0",
"temp12Alarm": "0",
"temp13Alarm": "0",
"temp14Alarm": "",
"temp15Alarm": "",
"temp16Alarm": "",
"al": "",
"bl": "",
"cl": "",
"ay": "",
"byy": "",
"cy": "",
"dh": "",
"sh": ""
},
"monitorMoreJsonData": null
},
"v2NetAliasname": null,
"v2NetAddress": null,
"devImg": null
},
{
"hostCode": "200000000045",
"realHostCode": null,
"hostName": "狗舍养殖45号",
"markerCode": null,
"markerName": "undefined",
"isOnLine": null,
"labelTime": null,
"addTime": null,
"attributeInfo": {
"equipmentOtherJsonData": null,
"equipmentParametersJsonData": null,
"equipmentWarnLimitJsonData": null,
"monitorBaseJsonData": {
"deviceId": "200000000045",
"storageName": null,
"dateTime": 1609503557000,
"online": 1,
"state": null,
"coolerState": null,
"alarmInfo": null,
"ramarks": {
"temp1Alias": "1温度1",
"temp1State": "1",
"temp1Synchro": "0",
"temp2Alias": "温度2冰箱",
"temp2State": "1",
"temp2Synchro": "0",
"temp3Alias": "温度3",
"temp3State": "0",
"temp3Synchro": "0",
"temp4Alias": "温度4",
"temp4State": "0",
"temp4Synchro": "0",
"temp5Alias": "46温度5",
"temp5State": "0",
"temp5Synchro": "0",
"temp6Alias": "湿度",
"temp6State": "0",
"temp6Synchro": "0",
"temp7Alias": "二氧化碳浓度",
"temp7State": "0",
"temp7Synchro": "0",
"temp8Alias": "氨气浓度",
"temp8State": "0",
"temp8Synchro": "0",
"temp9Alias": "11预留9",
"temp9State": "0",
"temp9Synchro": "0",
"temp10Alias": "预留10",
"temp10State": "0",
"temp10Synchro": "0",
"temp11Alias": "预留11",
"temp11State": "0",
"temp11Synchro": "0",
"temp12Alias": "预留12",
"temp12State": "0",
"temp12Synchro": "0",
"temp13Alias": "h预留13",
"temp13State": "0",
"temp13Synchro": "0",
"temp14Alias": "预留14",
"temp14State": "0",
"temp14Synchro": "0",
"temp15Alias": "预留15",
"temp15State": "0",
"temp15Synchro": "0",
"temp16Alias": "预留16",
"temp16State": "0",
"temp16Synchro": "0",
"DI0Alias": "风机1",
"DI0State": "0",
"DI0Synchro": "0",
"DI1Alias": "风机2",
"DI1State": "0",
"DI1Synchro": "0",
"DI2Alias": "风机3",
"DI2State": "0",
"DI2Synchro": "0",
"DI3Alias": "除湿机",
"DI3State": "0",
"DI3Synchro": "0",
"DI4Alias": "水帘",
"DI4State": "0",
"DI4Synchro": "0",
"DI5Alias": "现场声光报警",
"DI5State": "0",
"DI5Synchro": "0",
"DI6Alias": "DI6分机",
"DI6State": "0",
"DI6Synchro": "0",
"DI7Alias": "DI7分机",
"DI7State": "0",
"DI7Synchro": "0",
"DO0Alias": "外部报警1",
"DO0State": "1",
"DO0Synchro": "0",
"DO1Alias": "DO1---",
"DO1State": "0",
"DO1Synchro": "0",
"DO2Alias": "DO2报警",
"DO2State": "0",
"DO2Synchro": "0",
"DO3Alias": "DO3",
"DO3State": "0",
"DO3Synchro": "0",
"DO4Alias": "DO4",
"DO4State": "0",
"DO4Synchro": "0",
"DO5Alias": "DO5电机",
"DO5State": "0",
"DO5Synchro": "0",
"DO6Alias": "46DO6",
"DO6State": "0",
"DO6Synchro": "0",
"DO7Alias": "46DO7",
"DO7State": "0",
"DO7Synchro": "0"
},
"temp1": "23.0",
"temp2": "22.1",
"temp3": "21.3",
"temp4": "21.7",
"temp5": "",
"temp6": "0.0",
"temp7": "0",
"temp8": "0",
"temp9": "",
"temp10": "",
"temp11": "",
"temp12": "",
"temp13": "",
"temp14": "",
"temp15": "",
"temp16": "",
"dI0": "0",
"dI1": "0",
"dI2": "0",
"dI3": "0",
"dI4": "",
"dI5": "",
"dI6": "",
"dI7": "",
"dO0": "0",
"dO1": "0",
"dO2": "0",
"dO3": "0",
"dO4": "0",
"dO5": "0",
"dO6": "",
"dO7": "",
"temp1Alarm": "0",
"temp2Alarm": "0",
"temp3Alarm": "0",
"temp4Alarm": "0",
"temp5Alarm": "0",
"temp6Alarm": "",
"temp7Alarm": "0",
"temp8Alarm": "0",
"temp9Alarm": "",
"temp10Alarm": "",
"temp11Alarm": "",
"temp12Alarm": "",
"temp13Alarm": "",
"temp14Alarm": "",
"temp15Alarm": "",
"temp16Alarm": "",
"al": null,
"bl": null,
"cl": null,
"ay": null,
"byy": null,
"cy": null,
"dh": null,
"sh": null
},
"monitorMoreJsonData": null
},
"v2NetAliasname": null,
"v2NetAddress": null,
"devImg": null
}
]
}
]
}
]
}
|