krishna mohan
krishna mohan

Reputation: 267

unable to get json data in string variable from google api in c#, error does not contain a defination for getresponsense

i want to display 3 nodes values (VisibleUrl,titleNoFormatting,Content) values for a particular website. i got the response from google api. getting error in this line- foreach (var numberObj in req.getresponse) error- does not contain a defination for getresponse

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=www.flipkart.com

response

{
   "responseData":{
      "results":[
         {
            "GsearchResultClass":"GwebSearch",
            "unescapedUrl":"http://www.flipkart.com/",
            "url":"http://www.flipkart.com/",
            "visibleUrl":"www.flipkart.com",
            "cacheUrl":"http://www.google.com/search?q\u003dcache:kbwRsXJiOJMJ:www.flipkart.com",
            "title":"\u003cb\u003eFlipkart\u003c/b\u003e",
            "titleNoFormatting":"Flipkart",
            "content":"\u003cb\u003eFlipkart\u003c/b\u003e.\u003cb\u003ecom\u003c/b\u003e - India\u0026#39;s best website to buy wide range of products including \nElectronics, Books, Clothes, Accessories, Home furnishing and much more."
         },

aspx.cs

public partial class googlesearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string json = "";
        //getting json response from hamarasms api to insert sms delivery report
        if (Request.QueryString["data"] != null)
        {
            json = Request.QueryString["data"];
            var req = JsonConvert.DeserializeObject<RootObject>(json);
            string getresponse = req.responseData.ToString();

            foreach (var numberObj in req.getresponse)
            {
                var key = numberObj.visibleUrl;//Website name 
                var value = numberObj.titleNoFormatting;//website header
                var status = value.content;//website content
            }

        }
    }

}
public class Result
{
    public string GsearchResultClass { get; set; }
    public string unescapedUrl { get; set; }
    public string url { get; set; }
    public string visibleUrl { get; set; }
    public string cacheUrl { get; set; }
    public string title { get; set; }
    public string titleNoFormatting { get; set; }
    public string content { get; set; }
}

public class Page
{
    public string start { get; set; }
    public int label { get; set; }
}

public class Cursor
{
    public string resultCount { get; set; }
    public List<Page> pages { get; set; }
    public string estimatedResultCount { get; set; }
    public int currentPageIndex { get; set; }
    public string moreResultsUrl { get; set; }
    public string searchResultTime { get; set; }
}

public class ResponseData
{
    public List<Result> results { get; set; }
    public Cursor cursor { get; set; }
}

public class RootObject
{
    public ResponseData responseData { get; set; }
    public object responseDetails { get; set; }
    public int responseStatus { get; set; }
}

Upvotes: 1

Views: 113

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117281

I am not sure why you are using getresponse.

public class Result
{
    public string GsearchResultClass { get; set; }
    public string unescapedUrl { get; set; }
    public string url { get; set; }
    public string visibleUrl { get; set; }
    public string cacheUrl { get; set; }
    public string title { get; set; }
    public string titleNoFormatting { get; set; }
    public string content { get; set; }
}

public class Page
{
    public string start { get; set; }
    public int label { get; set; }
}

public class Cursor
{
    public string resultCount { get; set; }
    public List<Page> pages { get; set; }
    public string estimatedResultCount { get; set; }
    public int currentPageIndex { get; set; }
    public string moreResultsUrl { get; set; }
    public string searchResultTime { get; set; }
}

public class ResponseData
{
    public List<Result> results { get; set; }
    public Cursor cursor { get; set; }
}

public class RootObject
{
    public ResponseData responseData { get; set; }
    public object responseDetails { get; set; }
    public int responseStatus { get; set; }
}


RootObject tmp = JsonConvert.DeserializeObject<RootObject>(responseFromServer);

foreach (var result in tmp.responseData.results) {
    var key = result.visibleUrl;//Website name 
    var value = result.titleNoFormatting;//website header
    var status = result.content;    
    }

Upvotes: 1

Related Questions