JSON.Net and Linq

Posted by user1745143 on Stack Overflow See other posts from Stack Overflow or by user1745143
Published on 2012-10-14T20:53:00Z Indexed on 2012/10/14 21:37 UTC
Read the original article Hit count: 393

Filed under:
|

I'm a bit of a newbie when it comes to linq and I'm working on a site that parses a json feed using json.net. The problem that I'm having is that I need to be able to pull multiple fields from the json feed and use them for a foreach block. The documentation for json.net only shows how to pull just one field. I've done a few variations after checking out the linq documentation, but I've not found anything that works best. Here's what I've got so far:

 WebResponse objResponse;
    WebRequest objRequest = HttpWebRequest.Create(url);
    objResponse = objRequest.GetResponse();
    using (StreamReader reader = new StreamReader(objResponse.GetResponseStream()))
    {
        string json = reader.ReadToEnd();
        JObject rss = JObject.Parse(json);

        var postTitles =
            from p in rss["feedArray"].Children()
            select (string)p["item"], 
            //These are the fields I need to also query
            //(string)p["title"], (string)p["message"];

        //I've also tried this with console.write and labeling the field indicies for each pulled field
        foreach (var item in postTitles)
        {
            lbl_slides.Text += "<div class='slide'><div class='slide_inner'><div class='slide_box'><div class='slide_content'></div><!-- slide content --></div><!-- slide box --></div><div class='rotator_photo'><img src='" + item + "' alt='' /></div><!-- rotator photo --></div><!-- slide -->";
        }
    }

Has anyone seen how to pull multiple fields from a json feed and use them as part of a foreach block (or something similar?

© Stack Overflow or respective owner

Related posts about JSON

Related posts about json.net