2

I need to get following values of url from json

{
    "method":"create",
    "trace":"asdf/asdf/asdfasd",
    "order": {
        "ref":"asdfasdfasfasdfasd",
        "url":"https://domain.com/gateway/process.html?o=asdasdasdasd9"
    }
}

I am trying to get it using the following code:

var jsonData = (JObject)JsonConvert.DeserializeObject(str);
string url = jsonData["url"].Values<string>()

It is not working, I also tried JObject o = JObject.Parse(jsonData.ToString()); but I am not able to get it working either. (I get null exception for string url2 = jsonData["url"].Value<string>();)

Not sure what I am doing wrong

3
  • 1
    Define "not working". If you have error messages, show them. Commented Jan 18, 2017 at 11:59
  • 2
    is it not because the url property is in a nested object? Commented Jan 18, 2017 at 11:59
  • I get null exception for string url2 = jsonData["url"].Value<string>(); while i have the result Commented Jan 18, 2017 at 11:59

1 Answer 1

4

Your url is here:

string url = jsonData["order"]["url"].ToString();
Sign up to request clarification or add additional context in comments.

2 Comments

This is not working unless i add .Tostring(). This is the mistake i was makeing jsonData["order"]["url"].ToString()
thanks, i improved my answer. Havn't seen it in Linqpad, it worked there :)

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.