我想显示特定网站的3个节点值(VisibleUrl、titleNoFormatting、Content)。我收到了谷歌api的回复。此行中出现错误-foreach(req.getresponse中的var numberObj)错误-不包含getresponse 的定义
http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=www.flipkart.com
响应
{
"responseData":{
"results":[
{
"GsearchResultClass":"GwebSearch",
"unescapedUrl":"http://www.flipkart.com/",
"url":"http://www.flipkart.com/",
"visibleUrl":"www.flipkart.com",
"cacheUrl":"http://www.google.com/search?qu003dcache:kbwRsXJiOJMJ:www.flipkart.com",
"title":"u003cbu003eFlipkartu003c/bu003e",
"titleNoFormatting":"Flipkart",
"content":"u003cbu003eFlipkartu003c/bu003e.u003cbu003ecomu003c/bu003e - Indiau0026#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; }
}
我不知道为什么要使用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;
}