locked
Working with Json RRS feed

  • Question

  • User-1395793064 posted

     Fist of all, I have to idea where to put this thread. So if you, the admin, see better place for that go right a head and move it..

     

    I am working with Json object, but I dont know how to work with it..
    Thats the code:
     

    WebClient x = new WebClient();
    
    JsonTextParser pars = new JsonTextParser();
    JsonObject obj= pars.Parse(x.DownloadString(@"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=VW%20GOLF"));
    
     
    And the result in the obj is:

     {
        "responseData": {
            "results": [
                {
                    "GsearchResultClass": "GwebSearch",
                    "unescapedUrl": "http://en.wikipedia.org/wiki/Volkswagen_Golf",
                    "url": "http://en.wikipedia.org/wiki/Volkswagen_Golf",
                    "visibleUrl": "en.wikipedia.org",
                    "cacheUrl": "http://www.google.com/search?q=cache:zKwyv8FcFpMJ:en.wikipedia.org",
                    "title": "<b>Volkswagen Golf</b> - Wikipedia, the free encyclopedia",
                    "titleNoFormatting": "Volkswagen Golf - Wikipedia, the free encyclopedia",
                    "content": "The <b>Volkswagen Golf</b> is a hatchback / small family car manufactured by <b>Volkswagen</b>   since 1974 and marketed worldwide across six generations, in various body <b>...</b>"
                },
                {
                    "GsearchResultClass": "GwebSearch",
                    "unescapedUrl": "http://www.vw.com/",
                    "url": "http://www.vw.com/",
                    "visibleUrl": "www.vw.com",
                    "cacheUrl": "http://www.google.com/search?q=cache:7H3xgmo4OPUJ:www.vw.com",
                    "title": "<b>Volkswagen</b> of America",
                    "titleNoFormatting": "Volkswagen of America",
                    "content": "<b>Volkswagen</b> of America presents US vehicle information, pricing, incentives,   deals, comparisons on Eos, GTI, Jetta, New Beetle, New Beetle Convertible, <b>...</b>"
                },
                {
                    "GsearchResultClass": "GwebSearch",
                    "unescapedUrl": "http://www.vw.com/rabbit/en/us/",
                    "url": "http://www.vw.com/rabbit/en/us/",
                    "visibleUrl": "www.vw.com",
                    "cacheUrl": "http://www.google.com/search?q=cache:XmZPMSOLpc4J:www.vw.com",
                    "title": "<b>Volkswagen</b> Rabbit Home",
                    "titleNoFormatting": "Volkswagen Rabbit Home",
                    "content": "The portal to everything Rabbit - features and specs, photo gallery, special   offers - you can even build your own Rabbit and take it for a Test Drive from   <b>...</b>"
                },
                {
                    "GsearchResultClass": "GwebSearch",
                    "unescapedUrl": "http://www.edmunds.com/volkswagen/golf/review.html",
                    "url": "http://www.edmunds.com/volkswagen/golf/review.html",
                    "visibleUrl": "www.edmunds.com",
                    "cacheUrl": "http://www.google.com/search?q=cache:IalvGVlXkCkJ:www.edmunds.com",
                    "title": "<b>Volkswagen Golf</b> Review - Edmunds.com",
                    "titleNoFormatting": "Volkswagen Golf Review - Edmunds.com",
                    "content": "Early <b>Volkswagen Golf</b> models were popular thanks to their solid, precise feel on   the road and high-quality construction."
                }
            ],
            "cursor": {
                "pages": [
                    {
                        "start": "0",
                        "label": 1
                    },
                    {
                        "start": "4",
                        "label": 2
                    },
                    {
                        "start": "8",
                        "label": 3
                    },
                    {
                        "start": "12",
                        "label": 4
                    },
                    {
                        "start": "16",
                        "label": 5
                    },
                    {
                        "start": "20",
                        "label": 6
                    },
                    {
                        "start": "24",
                        "label": 7
                    },
                    {
                        "start": "28",
                        "label": 8
                    }
                ],
                "estimatedResultCount": "12500000",
                "currentPageIndex": 0,
                "moreResultsUrl": "http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=VW+GOLF"
            }
        },
        "responseDetails": null,
        "responseStatus": 200
    }

    Thursday, June 4, 2009 2:03 AM

Answers

  • User770037084 posted
    I would suggest using LINQ to JSON.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2009 11:10 PM
  • User770037084 posted

    You can use an anonymous type to clean that up:

    var items = from p in joResult["responseData"]["results"].Children()
    select new
    {
    Title = p.Value<string>("title"),
    URL = p.Value<string>("url"),
    Content = p.Value<string>("content")
    };

    foreach (var item in items)
    {
    // Use item.Title, item.URL, and item.Content here as you would expect.
    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 5, 2009 11:03 AM

All replies

  • User770037084 posted
    Do you want to work with this on the client-side or the server-side?
    Thursday, June 4, 2009 11:49 AM
  • User-1395793064 posted

     Server side

    Thursday, June 4, 2009 3:29 PM
  • User770037084 posted
    I would suggest using LINQ to JSON.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2009 11:10 PM
  • User-1395793064 posted

     Isnt there some object that can do:

    .responseData.results

    .getItem("responseData").getElements("results)

    Or something like that..?

     

    If I'll go on linq I need a proper object for that kind of query
     

    from p in joResult["responseData"]["results"].Children()
    select p.Value<string>("title"),p.Value<string>("url"),p.Value<string>("content");
    And no object can contain that kind of data, that I'm aware of. 

      So I did the following code:

     

    private void func()
        {
            WebClient x = new WebClient();
            JsonTextParser pars = new JsonTextParser();
            JsonObject obj= pars.Parse(x.DownloadString(@"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=VW%20GOLF"));
            JObject joResult = JObject.Parse(obj.ToString());
    
    
            var postTitles =
            from p in joResult["responseData"]["results"].Children()
            select p.Value<string>("title");
    
            var postURLs =
            from p in joResult["responseData"]["results"].Children()
            select p.Value<string>("url");
    
            var postContents =
            from p in joResult["responseData"]["results"].Children()
            select p.Value<string>("content");
    
            content.InnerHtml="";
    
            List<String> lTitles = postTitles.ToList<String>();
            List<String> lURLs = postURLs.ToList<String>();
            List<String> lContents = postContents.ToList<String>();
    
            for (int i = 0; i < lTitles.Count; i++)
    		{
                content.InnerHtml += "
    "title" + i + "\">" + lTitles[i] + "
    "; content.InnerHtml += "
    "URL" + i + "\">" + lURLs[i] + "
    "; content.InnerHtml += "
    "Content" + i + "\">" + lContents[i] + "
    "; } }

      

    but as you can see its ugly code. 3 select on the same json. 3 converts. theres got to be a better way

    Friday, June 5, 2009 2:56 AM
  • User770037084 posted

    You can use an anonymous type to clean that up:

    var items = from p in joResult["responseData"]["results"].Children()
    select new
    {
    Title = p.Value<string>("title"),
    URL = p.Value<string>("url"),
    Content = p.Value<string>("content")
    };

    foreach (var item in items)
    {
    // Use item.Title, item.URL, and item.Content here as you would expect.
    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 5, 2009 11:03 AM
  • User-1395793064 posted

     Thanks

    Friday, June 5, 2009 1:14 PM