26

I got a string containing Json. It looks like this:

"status_code":200,
"status_txt":"OK",
"data":
{
   "img_name":"D9Y3z.png",
   "img_url":"http:\/\/s1.uploads.im\/D9Y3z.png",
   "img_view":"http:\/\/uploads.im\/D9Y3z.png",
   "img_width":"167",
   "img_height":"288",
   "img_attr":"width=\"167\" height=\"288\"",
   "img_size":"36.1 KB",
   "img_bytes":36981,
   "thumb_url":"http:\/\/s1.uploads.im\/t\/D9Y3z.png",
   "thumb_width":360,
   "thumb_height":360,
   "source":"http:\/\/www.google.com\/images\/srpr\/nav_logo66.png",
   "resized":"0",
   "delete_key":"df149b075ab68c38"
}

I am trying to get a hold of the "img_url". I have Json.NET installed and I´ve found similar questions here..

for example something like this:

JObject o = JObject.Parse("{'People':[{'Name':'Jeff'},{'Name':'Joe'}]}");

// get name token of first person and convert to a string
string name = (string)o.SelectToken("People[0].Name");

In my case I changed ("People[0].Name") to ("img_url"),("img_url[0]) etc..no luck

This is my code now:

public string tempJson { get; set; }
public ActionResult SaveUploadedFile(string test)
{
    using (WebResponse wrs = wrq.GetResponse())
    using (Stream stream = wrs.GetResponseStream())
    using (StreamReader reader = new StreamReader(stream))
    {
        string json = reader.ReadToEnd();
        tempJson = json;
    }
}

Do I have to do something with the string before I can extract the value? Thanks!

6
  • Look closely at your JSON. The "img_url" property is part of a JSON object, which again is assigned to a JSON property. What is the name of that property? Commented Jun 15, 2014 at 19:08
  • You mean data? Im lost? Commented Jun 15, 2014 at 19:09
  • 3
    Yes, "data". Hence you should use SelectToken("data.img_url"); Commented Jun 15, 2014 at 19:10
  • Feel silly now..tried many combinations but not this one..Thanks! Commented Jun 15, 2014 at 19:12
  • 3
    I am in the process of writing an answer. Mark Sergey's answer, it is good. Pay attention to some differences however: Sergey's code will throw an exception if the "data" object is not in your JSON, whereas SelectToken will just return null (not sure whether this would become relevant for your application scenario) Commented Jun 15, 2014 at 19:18

2 Answers 2

44

img_url is not a property of root object - it's a property of data object:

var obj = JObject.Parse(json);
var url = (string)obj["data"]["img_url"]; // http://s1.uploads.im/D9Y3z.png

Another option:

var url = (string)obj.SelectToken("data.img_url");
Sign up to request clarification or add additional context in comments.

1 Comment

@elgonzo thanks. Reading properties with indexers does not involve parsing JPath, but I like SelectToken option for much better readability (and its more safe)
19

With help of this site

var obj = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(obj.data.img_url);

public class Data
{
    public string img_name { get; set; }
    public string img_url { get; set; }
    public string img_view { get; set; }
    public string img_width { get; set; }
    public string img_height { get; set; }
    public string img_attr { get; set; }
    public string img_size { get; set; }
    public int img_bytes { get; set; }
    public string thumb_url { get; set; }
    public int thumb_width { get; set; }
    public int thumb_height { get; set; }
    public string source { get; set; }
    public string resized { get; set; }
    public string delete_key { get; set; }
}

public class RootObject
{
    public int status_code { get; set; }
    public string status_txt { get; set; }
    public Data data { get; set; }
}

You can also do the same thing with the use of dynamic keyword (without declaring above classes)

dynamic obj = JsonConvert.DeserializeObject(json);
Console.WriteLine(obj.data.img_url);

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.