0

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.

4 Answers 4

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Spot on, Thank you.
0

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;

1 Comment

I did try something like this but didn't have much luck. Had a go with your example but i'm getting nothing back unfortunately.
0

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.

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.