【问题标题】:Loop JSON objects may not always exist循环 JSON 对象可能并不总是存在
【发布时间】:2016-09-12 23:01:34
【问题描述】:

我遇到了一个复杂的 JSON 对象。问题是有时,JSON 模型中的项目子列表可能不存在每个元素。 IE。完全不见了。

JSON 对象各不相同,对于返回所有内容的少数元素,我通常可以获得 2 或 3 个完整返回的项目,因此我知道我的对象模型至少有一部分是有效的,但例如 Name1来自以下模型的 em> 将成功返回。但随后解析 Name2 时出错。所以我似乎无法在没有异常的情况下遍历整个列表。

"Object reference not set to an instance of an object"

我尝试将输出包装在 Try Catch 块中,但仍然返回错误。

包含所有用例的 JSON 示例

[
  {
  "name": "Name1",
  "description": "",
  "location": "ANY",
  "inputs":       [
     {
        "name": "input1",
        "required": true,
        "description": "some short description"
     },
     {
        "name": "input2",
        "required": true,
        "description": "another short description"
     }
  ],
  "outputs":       [
     {
        "name": "output1",
        "required": false
     },
     {
        "name": "outpu2",
        "required": false
     }
  ]
 },
  {
   "name": "Name2",
   "description": "some long description",
   "location": "ANY",
   "inputs": [      {
        "name": "input1",
        "required": false,
        "description" : "some short description of the input"
  }]
  },
  {
     "name": "Name3",
     "description": "",
     "location": "ANY"
   }
 ]

我的 C# 定义适用于这个 JSON 对象的第一个引用,但是对于“Name2 和“Name3”,我得到这个“对象引用未设置为对象的实例”错误。

我的 C# Json 定义

 public class inputParameters
{
    [JsonProperty("name")]
    public string inputName { get; set; }

    [JsonProperty("required")]
    public bool inputRequired {get; set;}

    [JsonProperty("description")]
    public string inputDescription {get; set;}
}
public class outputParameters
{
    public string outputName { get; set; }
    public bool outputRequired { get; set; }
}
public class JsonObject
{
    [JsonProperty("name")]
    public string ProcessName { get; set; }

    [JsonProperty("description")]
    public string ProcessDescription { get; set; }

    [JsonProperty("peerLocation")]
    public string PeerLocation { get; set; }

    public List<inputParameters> InputParameters { get; set; }
    public List<outputParameters> OutputParameters{ get; set; }
}

还有我的反序列化对象和循环

var Object = JsonConvert.DeserializeObject <List<JsonObject>>(txt);

            foreach (JsonObject JsonObject in Object)
            {
                Console.WriteLine("Process Name: " + JsonObject.ProcessName);
                Console.WriteLine("PeerLoacatoin: " + JsonObject.PeerLocation);
                Console.WriteLine("Process Description: " +JsonObject.ProcessDescription);


                foreach (var InputParams in JsonObject.InputParameters)
                {
                    try
                    {
                        Console.WriteLine("Input Name: " + InputParams.inputName);
                        Console.WriteLine("Input Required: " + InputParams.inputRequired);
                        Console.WriteLine("Input Description: " + InputParams.inputDescription);
                    }

                    catch(Exception) 
                    {
                        InputParams.inputName = "";
                        InputParams.inputRequired = false;
                        InputParams.inputDescription = "";
                    }

                }
                foreach (var OutputParams in JsonObject.OutputParameters)
                {
                    try
                    {
                        Console.WriteLine("Output Name: " + OutputParams.outputName);
                        Console.WriteLine("Output Required: " + OutputParams.outputRequired);
                    }
                    catch (Exception)
                    {
                        OutputParams.outputName = "";
                        OutputParams.outputRequired = false;
                    }
                }
                Console.WriteLine();

【问题讨论】:

  • 你为什么要将它反序列化为 JsonObject 列表?将其反序列化为正确的 .NET 类型并对其进行操作不是更容易吗?
  • 这样做有什么好处?我将如何实施?

标签: c# json object


【解决方案1】:

我会创建一个构造函数,将你所有的Lists 初始化为new List。这样您就可以安全地进行迭代而不会出现空指针异常。

class JsonObject
{
    public JsonObject()
    {
        InputParameters = new List<inputParameters>();
        OutputParameters = new List<outputParameters>();
    }
}

【讨论】:

  • 谢谢!有用!公共类 JsonObject { ... public List InputParameters { get;放; } public List OutputParameters { get;放; } public JsonObject () { InputParameters = new List(); OutputParameters = new List(); } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
  • 2020-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多