反序列化JSON返回null C# [英] DeSerializing JSON returns null C#
问题描述
我正在尝试反序列化以下JSON.
I have following JSON that I'm trying to deserialize.
{
"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
I have created following classes to bind the 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; }
}
反序列化时没有任何错误.我也可以轻松浏览.
I don't get any error while i'm deserializing it. Also i can navigate through easily.
但是当我尝试获取element [n]的值时(output_parameters [0] .value.array_.element [0] .value).它返回null.
but when i,m trying to get the value of element[n] (output_parameters[0].value.array_.element[0].value). it returns null.
这是什么问题?
注意:我只需要从JSON中获取几个值即可.例如,我不需要像 "type":"string", "name":错误", "scope":"local" 这就是为什么我创建像这样的C#类的原因.
NOTE : I Just need few values to be taken from JSON. For example I don't need values like "type":"string", "name":"Error", "scope":"local" That's why i created C# classes like this.
推荐答案
您的类应如下图所示,因为output-parameters
是数组[
,并且它可能包含更多值或列表,因此请创建一个包含List的Rootobject
输出参数.
Your Class should look like this since output-parameters
is a array [
and it might contain more values or list so create a Rootobject
which contains List of output-parameters.
现在output-parameters
不仅包含value
和name
,而且还包含. type
和scope
位于相同的层次结构.
Now output-parameters
not only contains value
and name
whereas it also contains. type
and scope
at the same hierarchy.
您没有对名为string
的节点的声明,该声明在这里被称为SomeString
和SomeString1
You have no declaration for node called string
which is called here as SomeString
and SomeString1
您的班级值还包含string
,在此表示为SomeString1
Your class Value also contains string
which is denoted here as 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; }
}
然后您应该使用以下代码反序列化
Then you should deserialize it using the following code
Rootobject ro = JsonConvert.DeserializeObject<Rootobject>(jsonstr);
Console.WriteLine(ro.outputparameters[0].value.array.elements[0]._string.value);
显示已提取值的屏幕截图
Screenshot which shows the value has been extracted
这篇关于反序列化JSON返回null C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!