【问题标题】:DeSerializing JSON returns null C#反序列化 JSON 返回 null C#
【发布时间】:2016-11-25 09:24:35
【问题描述】:

我正在尝试反序列化以下 JSON。

{  
"output-parameters":[  
  {  
     "value":{  
        "array":{  
           "elements":[  
              {  
                 "string":{  
                    "value":"cp-100"
                 }
              },
              {  
                 "string":{  
                    "value":"cp-101"
                 }
              },
              {  
                 "string":{  
                    "value":"cp-100"
                 }
              },
              {  
                 "string":{  
                    "value":"cp-101"
                 }
              },
              {  
                 "string":{  
                    "value":"cp-100"
                 }
              }
           ]
        }
     },
     "type":"Array/string",
     "name":"Tags",
     "scope":"local"
  },
  {  
     "value":{  
        "string":{  
           "value":"subscribed"
        }
     },
     "type":"string",
     "name":"Error",
     "scope":"local"
  }
]
}

我创建了以下类来绑定 JSON

 public class OutputParameter
{
    [JsonProperty(PropertyName = "value")]
    public value value { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string name { get; set; }
}

public class value
{
    [JsonProperty(PropertyName = "array")]
    public array_ array_ { get; set; }
}

public class array_
{
    [JsonProperty(PropertyName = "elements")]
    public element[] element { get; set; }
}

public class element
{
    [JsonProperty(PropertyName = "value")]
    public string value { get; set; }
}

我在反序列化它时没有收到任何错误。我也可以轻松浏览。

但是当我试图获取 element[n] 的值时(output_parameters[0].value.array_.element[0].value)。它返回 null。

这里有什么问题?

注意:我只需要从 JSON 中获取几个值。例如,我不需要像 “类型”:“字符串”, "名称":"错误", “范围”:“本地” 这就是我创建这样的 C# 类的原因。

【问题讨论】:

  • 你不应该寻找“output_parameters[0].value.array_.element[0].string.value”吗?
  • 尝试使用json2csharp.com 从 JSON 生成 C# 类?

标签: c# json json.net deserialization


【解决方案1】:

你的类应该是这样的,因为output-parameters 是一个数组[,它可能包含更多的值或列表,所以创建一个包含输出参数列表的Rootobject。

现在output-parameters 不仅包含value 和name,而且还包含。 type 和 scope 在同一层次结构中。

您没有声明名为 string 的节点,此处称为 SomeString 和 SomeString1

您的类值还包含string,此处表示为SomeString1

public class Rootobject
{
    [JsonProperty(PropertyName = "output-parameters")]
    public List<OutputParameters> outputparameters { get; set; }
}
public class OutputParameters
{
    [JsonProperty(PropertyName = "value")]
    public SomeValue value { get; set; }

    [JsonProperty(PropertyName = "type")]
    public string type { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string name { get; set; }

    [JsonProperty(PropertyName = "scope")]
    public string scope { get; set; }
}
public class SomeValue
{
    [JsonProperty(PropertyName = "array")]
    public SomeArray array { get; set; }

    [JsonProperty(PropertyName = "string")]
    public SomeString1 _string { get; set; }
}
public class SomeArray
{
    [JsonProperty(PropertyName = "elements")]
    public List<SomeElement> elements { get; set; }
}
public class SomeElement
{
    [JsonProperty(PropertyName = "string")]
    public SomeString _string { get; set; }
}
public class SomeString
{
    [JsonProperty(PropertyName = "value")]
    public string value { get; set; }
}
public class SomeString1
{
    [JsonProperty(PropertyName = "value")]
    public string value { get; set; }
}

那么你应该使用下面的代码反序列化它

Rootobject ro = JsonConvert.DeserializeObject<Rootobject>(jsonstr);
Console.WriteLine(ro.outputparameters[0].value.array.elements[0]._string.value);            

显示已提取值的屏幕截图

【讨论】:

  • 并且公共属性应该是PascalCase
  • @ManfredRadlwimmer:这不是必需的,因为它是命名约定的标准之一。
  • 这也说明了同样的事情.. 值为 null :(
  • 现在需要更新或编辑反序列化的代码。
  • 请检查我添加的注释 :) ...我已通过添加类调用“字符串”进行检查 .... public class element { [JsonProperty(PropertyName = "string")] public string value { 得到;放; } }...........但这会返回一个错误,说没有找到名称为“字符串”的标签
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多