Reputation: 148
I have a JSON array that is converted from XML and i'd like to know how I can get parts of that JSON.
From another answer i've found
var result =
JObject.Parse(jsonResult).Children().Children().Children().ElementAt(1).Children().First();
but that just gets me one part of the JSON and isn't very easy to figure out how to then get other parts.
This is the part I get from the code above
http://www.w3.org/2001/XMLSchema-instance
This is the JSON
{
"soap12:Envelope": {
"@xmlns:soap12": "http://www.w3.org/2003/05/soap-envelope",
"@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"@xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"soap12:Body": {
"ProcessRequestResponse": {
"@xmlns": "http://localhost/TestServices",
"ProcessRequestResult": {
"StatusCode": "None or GE or PE or PW or NP or FS or NA or GF",
"Success": "boolean",
"Errors": {
"Error": [
{
"Code": "int",
"ErrorText": "string",
"ErrorType": "None or Critical or Non_Critical",
"Severity": "Warning or Error"
},
{
"Code": "int",
"ErrorText": "string",
"ErrorType": "None or Critical or Non_Critical",
"Severity": "Warning or Error"
}
]
}
}
}
}
}
}
I would like to be able to get "StatusCode" or "Success" or anything in the array.
Upvotes: 0
Views: 103
Reputation: 22876
Deserializing to a class is usually the best approach, but another alternative is JSONPath query :
var jo = JObject.Parse(json);
string StatusCode = (string)jo.SelectToken("$..StatusCode");
string[] ErrorCodes = jo.SelectTokens("$..Code").Select(t => (string)t).ToArray();
Upvotes: 0
Reputation: 21
To get them with JObject.Parse()
string jsonresult = "{\"soap12:Envelope\":{\"@xmlns:soap12\":\"http://www.w3.org/2003/05/soap-envelope\",\"@xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\",\"@xmlns:xsd\":\"http://www.w3.org/2001/XMLSchema\",\"soap12:Body\":{\"ProcessRequestResponse\":{\"@xmlns\":\"http://localhost/MeetingBrokerServices\",\"ProcessRequestResult\":{\"StatusCode\":\"None or PAR or PNE or PWE or NPE or FSR or NAC or TF\",\"Success\":\"boolean\",\"Errors\":{\"Error\":[{\"Code\":\"int\",\"ErrorText\":\"string\",\"ErrorType\":\"None or Critical or Non_Critical\",\"Severity\":\"Warning or Error\"},{\"Code\":\"int\",\"ErrorText\":\"string\",\"ErrorType\":\"None or Critical or Non_Critical\",\"Severity\":\"Warning or Error\"}]}}}}}}";
var result = JObject.Parse(jsonresult);
Console.WriteLine(result["soap12:Envelope"]["soap12:Body"]["ProcessRequestResponse"]["ProcessRequestResult"]["Success"]);
returns 'boolean' as result. However do use DavidG's suggestion as this is obviously not very neat.
Upvotes: 0
Reputation: 2689
DavidG answer is good enough but in case you want to know how to access json data then use JObject
var jo = JObject.Parse(myJsonString);
var status = jo["soap12:Envelope"]["Success"];
Namespace is using Newtonsoft.Json.Linq;
Upvotes: 0
Reputation: 119016
I would recommend not using JObject.Parse
directly and instead deserialise directly to your own class hierarchy. For example, with a simple set of classes like this:
public class SoapObject
{
[JsonProperty("soap12:Envelope")]
public SoapData Envelope { get; set; }
}
public class SoapData
{
[JsonProperty("soap12:Body")]
public SoapBody Body { get; set; }
}
public class SoapBody
{
public ProcessRequestResponse ProcessRequestResponse { get; set; }
}
public class ProcessRequestResponse
{
public ProcessRequestResult ProcessRequestResult { get; set; }
}
public class ProcessRequestResult
{
public string StatusCode { get; set; }
public string Success { get; set; }
}
You could deserialise simply:
var soapObject = JsonConvert.DeserializeObject<SoapObject>(jsonResult);
And now you have strongly typed access to the properties you need:
var statusCode = soapObject.Envelope.Body
.ProcessRequestResponse.ProcessRequestResult.StatusCode;
Upvotes: 3