如何在Unity中进行JSON解析?
How to do JSON parsing in Unity?
我有一个字符串变量,其中包含以下 JSON。
[
{
"MeterLookup_TblRefID": 233,
"NetworkLookup_TblRefID": 3,
"Network_Name": "SS-43 SEWA SCADA (Command Center)",
"Longitude": 55.403544,
"Latitude": 25.366666,
"OnOFfStatus": true
},
{
"MeterLookup_TblRefID": 297,
"NetworkLookup_TblRefID": 4,
"Network_Name": "SS-8 MaySaloon",
"Longitude": 55.406274,
"Latitude": 25.360654,
"OnOFfStatus": true
},
{
"MeterLookup_TblRefID": 298,
"NetworkLookup_TblRefID": 5,
"Network_Name": "SS-1 Al Nasserya Driving School",
"Longitude": 55.404669,
"Latitude": 25.367591,
"OnOFfStatus": true
}
]
I want to parse it to a JSON object in unity
。任何人都可以告诉我怎么做。
您可以使用 Unity 的 JSONUtility.
此示例取自 Unity 的文档本身。
using UnityEngine;
[System.Serializable]
public class PlayerInfo
{
public string name;
public int lives;
public float health;
public static PlayerInfo CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<PlayerInfo>(jsonString);
}
// Given JSON input:
// {"name":"Dr Charles","lives":3,"health":0.8}
// this example will return a PlayerInfo object with
// name == "Dr Charles", lives == 3, and health == 0.8f.
}
参考:https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html
使用这个函数:
public static T[] DeserializeFromJsonArray<T>(string jsonString)
{
string newJson = "{ \"array\": " + jsonString + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
return wrapper.array;
}
像这样:
JsonObjectModel[] arrayOfObjects = DeserializeFromJsonArray<JsonObjectModel>(jsonArrayString);
如果您想再次序列化,请使用:
public static string SerializeToJsonArray<T>(T[] arrayToSerialize)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.array = arrayToSerialize;
return JsonUtility.ToJson(wrapper);
}
并像这样使用它:
string jsonString = JsonManager.SerializeToJson<JsonArrayModel>(jsonArrayModel);
如果您有更多问题,我得到了一些 UnityUtils 的回购协议,其中包含此 JsonManaging 的示例:https://github.com/EricBatlle/SimpleUnityUtils/tree/master/Assets/Simple_JsonManager
我有一个字符串变量,其中包含以下 JSON。
[
{
"MeterLookup_TblRefID": 233,
"NetworkLookup_TblRefID": 3,
"Network_Name": "SS-43 SEWA SCADA (Command Center)",
"Longitude": 55.403544,
"Latitude": 25.366666,
"OnOFfStatus": true
},
{
"MeterLookup_TblRefID": 297,
"NetworkLookup_TblRefID": 4,
"Network_Name": "SS-8 MaySaloon",
"Longitude": 55.406274,
"Latitude": 25.360654,
"OnOFfStatus": true
},
{
"MeterLookup_TblRefID": 298,
"NetworkLookup_TblRefID": 5,
"Network_Name": "SS-1 Al Nasserya Driving School",
"Longitude": 55.404669,
"Latitude": 25.367591,
"OnOFfStatus": true
}
]
I want to parse it to a JSON object in unity
。任何人都可以告诉我怎么做。
您可以使用 Unity 的 JSONUtility.
此示例取自 Unity 的文档本身。
using UnityEngine;
[System.Serializable]
public class PlayerInfo
{
public string name;
public int lives;
public float health;
public static PlayerInfo CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<PlayerInfo>(jsonString);
}
// Given JSON input:
// {"name":"Dr Charles","lives":3,"health":0.8}
// this example will return a PlayerInfo object with
// name == "Dr Charles", lives == 3, and health == 0.8f.
}
参考:https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html
使用这个函数:
public static T[] DeserializeFromJsonArray<T>(string jsonString)
{
string newJson = "{ \"array\": " + jsonString + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
return wrapper.array;
}
像这样:
JsonObjectModel[] arrayOfObjects = DeserializeFromJsonArray<JsonObjectModel>(jsonArrayString);
如果您想再次序列化,请使用:
public static string SerializeToJsonArray<T>(T[] arrayToSerialize)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.array = arrayToSerialize;
return JsonUtility.ToJson(wrapper);
}
并像这样使用它:
string jsonString = JsonManager.SerializeToJson<JsonArrayModel>(jsonArrayModel);
如果您有更多问题,我得到了一些 UnityUtils 的回购协议,其中包含此 JsonManaging 的示例:https://github.com/EricBatlle/SimpleUnityUtils/tree/master/Assets/Simple_JsonManager