反序列化JSON返回null C#
我正在尝试反序列化以下JSON反序列化JSON返回null C#,c#,json,json.net,deserialization,C#,Json,Json.net,Deserialization,我正在尝试反序列化以下JSON { "output-parameters":[ { "value":{ "array":{ "elements":[ { "string":{ "value":"cp-100" } }, {
{
"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; }
}
我在反序列化时没有收到任何错误。我也可以轻松地浏览
但当我尝试获取元素[n]的值时(输出参数[0].value.array\uu.element[0].value)。它返回null
这里有什么问题
注意:我只需要从JSON中获取一些值。例如,我不需要像这样的值
“类型”:“字符串”,
“名称”:“错误”,
“范围”:“本地”
这就是为什么我创建了这样的C类。您的类应该是这样的,因为
输出参数
是一个数组[
,它可能包含更多的值或列表,所以创建一个包含输出参数列表的根对象
现在,输出参数
不仅包含值
和名称
,而且在同一层次结构中还包含。类型
和范围
您没有对名为string
的节点进行声明,该节点在此处称为SomeString
和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; }
}
您的类值还包含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; }
}
公共类根对象
{
[JsonProperty(PropertyName=“输出参数”)]
公共列表输出参数{get;set;}
}
公共类OutputParameters
{
[JsonProperty(PropertyName=“value”)]
公共值{get;set;}
[JsonProperty(PropertyName=“type”)]
公共字符串类型{get;set;}
[JsonProperty(PropertyName=“name”)]
公共字符串名称{get;set;}
[JsonProperty(PropertyName=“scope”)]
公共字符串作用域{get;set;}
}
公共类值
{
[JsonProperty(PropertyName=“array”)]
公共数组{get;set;}
[JsonProperty(PropertyName=“string”)]
公共SomeString1_字符串{get;set;}
}
公共类数组
{
[JsonProperty(PropertyName=“elements”)]
公共列表元素{get;set;}
}
公共类元素
{
[JsonProperty(PropertyName=“string”)]
公共SomeString _string{get;set;}
}
公共类字符串
{
[JsonProperty(PropertyName=“value”)]
公共字符串值{get;set;}
}
公共类1
{
[JsonProperty(PropertyName=“value”)]
公共字符串值{get;set;}
}
然后应该使用以下代码对其进行反序列化
Rootobject ro = JsonConvert.DeserializeObject<Rootobject>(jsonstr);
Console.WriteLine(ro.outputparameters[0].value.array.elements[0]._string.value);
Rootobject ro=JsonConvert.DeserializeObject(jsonstr);
Console.WriteLine(ro.outputparameters[0].value.array.elements[0].\u string.value);
显示已提取值的屏幕截图
您的类应该是这样的,因为输出参数
是一个数组[
,它可能包含更多的值或列表,所以创建一个包含输出参数列表的根对象
现在,输出参数
不仅包含值
和名称
,而且在同一层次结构中还包含。类型
和范围
您没有对名为string
的节点进行声明,该节点在此处称为SomeString
和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; }
}
您的类值还包含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; }
}
公共类根对象
{
[JsonProperty(PropertyName=“输出参数”)]
公共列表输出参数{get;set;}
}
公共类OutputParameters
{
[JsonProperty(PropertyName=“value”)]
公共值{get;set;}
[JsonProperty(PropertyName=“type”)]
公共字符串类型{get;set;}
[JsonProperty(PropertyName=“name”)]
公共字符串名称{get;set;}
[JsonProperty(PropertyName=“scope”)]
公共字符串作用域{get;set;}
}
公共类值
{
[JsonProperty(PropertyName=“array”)]
公共数组{get;set;}
[JsonProperty(PropertyName=“string”)]
公共SomeString1_字符串{get;set;}
}
公共类数组
{
[JsonProperty(PropertyName=“elements”)]
公共列表元素{get;set;}
}
公共类元素
{
[JsonProperty(PropertyName=“string”)]
公共SomeString _string{get;set;}
}
公共类字符串
{
[JsonProperty(PropertyName=“value”)]
公共字符串值{get;set;}
}
公共类1
{
[JsonProperty(PropertyName=“value”)]
公共字符串值{get;set;}
}
然后应该使用以下代码对其进行反序列化
Rootobject ro = JsonConvert.DeserializeObject<Rootobject>(jsonstr);
Console.WriteLine(ro.outputparameters[0].value.array.elements[0]._string.value);
Rootobject ro=JsonConvert.DeserializeObject(jsonstr);
Console.WriteLine(ro.outputparameters[0].value.array.elements[0].\u string.value);
显示已提取值的屏幕截图
是否应该查找“输出参数[0].value.array.element[0].string.value”?是否尝试使用从JSON生成C类?是否应该查找“输出参数[0].value.array.element[0].string.value”?尝试使用从JSON生成C#类?公共属性应在PascalCase@ManfredRadlwimmer:这是不必要的,因为它是命名约定的标准之一。这也说明了同样的事情..值为null:(现在需要更新或编辑反序列化代码。请检查我添加的注释:)…我已经通过添加类调用“string”进行了检查。…公共类元素{[JsonProperty(PropertyName=“string”)]公共字符串值{get;set;}}………..。但这返回一个错误,表示找不到名为“string”的标记公共财产也应该在PascalCase@ManfredRadlwimmer:这是不必要的,因为它是命名约定的标准之一。这也说明了同样的事情..Value为null:(现在需要更新或编辑反序列化代码。请检查我添加的注释:)…我已通过添加类调用“string”进行检查…p