问题
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"
}
]
}
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.
but when i,m trying to get the value of element[n] (output_parameters[0].value.array_.element[0].value). it returns null.
What is the problem here ?
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.
回答1:
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.
Now output-parameters not only contains value and name whereas it also contains. type and scope at the same hierarchy.
You have no declaration for node called string which is called here as SomeString and 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
来源:https://stackoverflow.com/questions/40801494/deserializing-json-returns-null-c-sharp