【发布时间】:2019-05-17 20:03:54
【问题描述】:
Unity 中是否有办法将 JSON 字符串解析为 C# 类,其中包含 - 让我们假设 - 字符串列表?
我有一个从服务器获取大量数据的应用程序。该数据以 JSON 格式出现,并且有很多字段。有些字段中还有另一个数组(我知道里面有数组的和没有数组的)。
知道了这一点,我想把它变成一个类,这样我就可以访问它的参数,[我不能创建一个数组并尝试访问 - 例如 - arr[0],因为 arr[0] 可能不是我想要的字段 - 假设数组位置可以更改,但字段键(或字段字段键,如果数组内部有数组)值不会]
{
"success": "1",
"menus": [
{
"id_menu": "506",
"nome": "Blog",
"tipo": "blog"
},
{
"id_menu": "510",
"nome": "Sobre",
"tipo": ""
},
{
"id_menu": "2407",
"nome": "(Projeto Quatro Artes)",
"tipo": ""
},
{
"id_menu": "2603",
"nome": "(Loja)",
"tipo": ""
},
{
"id_menu": "2687",
"nome": "(Material NA)",
"tipo": ""
},
{
"id_menu": "2818",
"nome": "(Forms)",
"tipo": ""
},
{
"id_menu": "2826",
"nome": "(PHP+PIZZA)",
"tipo": ""
},
{
"id_menu": "7728",
"nome": "(Image)",
"tipo": ""
},
{
"id_menu": "3239",
"nome": "(jc)",
"tipo": ""
},
{
"id_menu": "6024",
"nome": "(assinatura)",
"tipo": ""
}
]
}
上面的代码是我要解析的 JSONS 之一,下面的代码是我用来解析它的类。现在我只需要知道该怎么做。
[System.Serializable]
public class Menu
{
public string id_menu { get; set; }
public string nome { get; set; }
public string tipo { get; set; }
}
[System.Serializable]
public class Return
{
public string success { get; set; }
public List<Menu> menus { get; set; }
}
我知道如果没有任何列表,JsonUtility.FromJson() 可以完成这项工作,但如果有列表,它就不起作用 此外,尝试使用 Unity 资产中的 JSONObject 也不起作用,因为我无法访问 return.menus.tipo(例如),因为它没有关联索引。
【问题讨论】: