循环JSON对象可能并不总是存在

时间:2016-09-12 14:47:02

标签: c# json object

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

JSON对象各不相同,对于返回所有内容的少数元素,我通常可以获得2或3个完整的返回项目,因此我知道至少某些部分的对象模型,但有效但例如 Name1 将成功返回。但是然后解析 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();

1 个答案:

答案 0 :(得分:1)

我会创建一个构造函数,将所有List初始化为new List。这样,您可以安全地迭代而不会获得空指针异常。

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