I get following JSON where i need to extract few data from it.
{
"id":"400xxtc200",
"state":"failed",
"name":"barbaPapa",
"content-exception":"AccessDenied",
"input-parameters":[
{
"value":{
"string":{
"value":"In"
}
},
"type":"string",
"name":"Operation",
"scope":"local"
},
{
"value":{
"string":{
"value":"bila"
}
},
"type":"string",
"name":"VMName",
"scope":"local"
},
{
"value":{
"string":{
"value":"txtc"
}
},
"type":"string",
"name":"PSUser",
"scope":"local"
},
{
"value":{
"string":{
"value":"dv1"
}
},
"type":"string",
"name":"Datacenter",
"scope":"local"
},
{
"value":{
"string":{
"value":"tpc"
}
},
"type":"string",
"name":"ServiceArea",
"scope":"local"
},
{
"value":{
"string":{
"value":"103"
}
},
"type":"string",
"name":"SQN",
"scope":"local"
}
],
"output-parameters":[
{
"type":"Array/string",
"name":"tag",
"scope":"local"
},
{
"value":{
"string":{
"value":"AccessDenied"
}
},
"type":"string",
"name":"Error",
"scope":"local"
}
]
}
I am trying to deserialize the JSON object in to a dynamic object which was successful.I'm using following C# code for the work
string responseText = reader.ReadToEnd();
dynamic in_values = JsonConvert.DeserializeObject(responseText);
string state = in_values.state;
If you can see I have some tag names with hyphen in the JSON string.
For example output-parameters
I cant use the dot operate because then it will be as like follow
in_values.output-parameters;
How to extract those values from the JSON string.
You can use JsonProperty
in this case.
public class SampleClass
{
public string id { get; set; }
public string state { get; set; }
public string name { get; set; }
[JsonProperty(PropertyName = "content-exception")]
public string content_exception { get; set; }
[JsonProperty(PropertyName = "input-parameters")]
public List<InputParameter> input_parameters { get; set; }
[JsonProperty(PropertyName = "output-parameters")]
public List<OutputParameter> output_parameters { get; set; }
}
string json = File.ReadAllText("abc.txt");
SampleClass obj = JsonConvert.DeserializeObject<SampleClass>(json);
List<OutputParameter> ls = obj.output_parameters;