【问题标题】:Is there a way to convert a JSON to C# class who have a List<Object> as a parameter?有没有办法将 JSON 转换为具有 List<Object> 作为参数的 C# 类?
【发布时间】: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(例如),因为它没有关联索引。

【问题讨论】:

    标签: c# json unity3d


    【解决方案1】:

    您可以通过以下方式进行:

    var returnObj = JsonConvert.DeserializeObject<Return>(json);
    

    为了使其工作,您需要以下包:Newtonsoft.Json

    结果如下:

    我制作了一个小型控制台应用程序,它为您指明了方向。在 Unity 中应该是一样的:

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace IoException
    {
        [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; }
        }
    
        public class JsonExercise
        {
            public static void Main(string[] args)
            {
                var currentLine = Console.ReadLine();
                var sb = new StringBuilder();
    
                while (currentLine != "END")
                {
                    sb.AppendLine(currentLine);
    
                    currentLine = Console.ReadLine();
                }
    
                var json = sb.ToString().Trim();
    
                var returnObj = JsonConvert.DeserializeObject<Return>(json);
    
                Console.WriteLine();
            }
        }
    }
    

    有关控制台应用程序的说明,以防您想尝试:

    • 输入 JSON 时,在最后一行输入“END”,以便控制台应用停止读取。
    • 对于输入 JSON,我使用了您提供的那个。

    【讨论】:

    【解决方案2】:

    也许使用Json.NET 或其他完全支持任意类型的反序列化器?

    public class MyClass
    {
        public string id_menu { get; set; }
        public string nome { get; set; }
        public string tipo { get; set; }
        public List<string> my_list {get; set; }
    }
    
    var result = JsonConvert.DeserializeObject<MyClass>(json);
    

    【讨论】:

    • 感谢您的回答!但不幸的是 Unity3D 不允许我使用 Newtonsoft 库。它与 Unity 中的类有很多冲突,无法修复。
    【解决方案3】:

    如果您尝试通过 Unity 解析 Json,您可以使用 JsonUtility.FromJson(jsonString),它会将 Json 解析为可以处理的对象,但我从未尝试使用复杂的 JSON。

    或者你可以运行 json 并搜索你想要的键值,但它确实会增加内存消耗。

    【讨论】:

    • 是的,由于某种原因它不起作用,值“成功”和列表什么都没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多