繁体   English   中英

反序列化JSON返回null C#

[英]DeSerializing JSON returns null C#

我正在尝试反序列化以下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中获取少量值即可。 例如,我不需要像“ type”:“ string”,“ name”:“ Error”,“ scope”:“ local”这样的值,这就是为什么我创建了这样的C#类的原因。

您的类应该看起来像这样,因为output-parameters是一个数组[并且它可能包含更多值或列表,因此创建一个包含output-parameters列表的Rootobject

现在, output-parameters不仅包含valuename ,而且还包含。 typescope位于同一层次结构。

您没有声明名为string节点的声明,在此称为SomeStringSomeString1

您的类Value还包含在此处表示为SomeString1 string

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);            

屏幕截图显示了该值已被提取

屏幕截图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM