【发布时间】:2015-03-14 00:19:55
【问题描述】:
我有一个问题,因为我是 Bing Search API 的新手,不熟悉如何使用它。我正在尝试从 Bing 搜索结果中获取所有链接。所以我正在搜索关键字。正在工作,但我想获取从我的 Java 应用程序中包含的 Bing 搜索 API 获得的结果的链接。问题是我想检索链接并将其保存到数组中。所以我使用 XML 将其解析为 JSON。但是,当我尝试获取 URL 或链接时,我无法获取它们的主要问题。有谁知道该怎么做或我做错了什么?
例如我想获取http://en.wikipedia.org/wiki/Omonoia(必应搜索API的搜索结果之一)
以下是部分代码:
String str = "http://maps.google.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";
URL url = new URL(str);
InputStream is = url.openStream();
int ptr = 0;
StringBuilder builder = new StringBuilder();
while ((ptr = is.read()) != -1) {
builder.append((char) ptr);
}
String xml = builder.toString();
JSONObject jsonObject = XML.toJSONObject(xml);
System.out.println(jsonObject.toString());
System.out.println(jsonObject.get("id"));
一些输出:
"feed":{"entry":[{"id":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=' what is omonoia'&$skip=0&$top=1","title":{"type":"text","content":"WebResult"},"updated":"2015-01-15T14:35:57Z","content":{"m:properties":{"d:Url":{"content":"http://en.wikipedia.org/wiki/AC_Omonia","m:type":"Edm.String"},"d:DisplayUrl":{"content":"en.wikipedia.org/wiki/AC_Omonia","m:type":"Edm.String"},"d:Title":{"content":"AC Omonia - Wikipedia, the free encyclopedia","m:type":"Edm.String"},"d:Description":{"content":"Athletic Club Omonoia Nicosia, commonly referred to as Omonoia, is a Cypriot professional football club based in the capital city, Nicosia. The club was established ...","m:type":"Edm.String"},"d:ID":{"content":"88cf85ab-f077-4f2b-8037-f3d3447b9d34","m:type":"Edm.Guid"}},"type":"application/xml"}},{"id":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=' what is omonoia'&$skip=1&$top=1","title":{"type":"text","content":"WebResult"},"updated":"2015-01-15T14:35:57Z","content":{"m:properties":{"d:Url":{"content":"http://en.wikipedia.org/wiki/Omonoia","m:type":"Edm.String"},"d:DisplayUrl":{"content":"en.wikipedia.org/wiki/Omonoia","m:type":"Edm.String"},"d:Title":{"content":"Omonoia - Wikipedia, the free encyclopedia","m:type":"Edm.String"},"d:Description":{"content":"Omonoia may refer to: Omonoia Square, one of Athens' main squares, Omonoia Station, the subway station located on the square or Omonoia, the neighborhood around it.","m:type":"Edm.String"},"d:ID":{"content":"4668962f-c8cb-43b1-a12d-19d8aee944bb","m:type":"Edm.Guid"}},"type":"application/xml"}},{"id":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=' what is omonoia'&$skip=2&$top=1","title":{"type":"text","content":"WebResult"},"updated":"2015-01-15T14:35:57Z","content":{"m:properties":{"d:Url":{"content":"http://wikitravel.org/en/Athens/Omonia","m:type":"Edm.String"},"d:DisplayUrl":{"content":"wikitravel.org/en/Athens/Omonia","m:type":"Edm.String"},"d:Title":{"content":"Athens/Omonia - Wikitravel - The Free Travel Guide","m:type":"Edm.String"},"d:Description":{"content":"Omonia Square is the center of Athens, and is composed of the actual square together with the surrounding streets, open areas and assemblage of grand buildings that ...","m:type":"Edm.String"},"d:ID":{"content":"b711b509-d478-48c4-ad44-51e9d87a5646","m:type":"Edm.Guid"}},"type":"application/xml"}},{"id":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=' what is omonoia'&$skip=3&$top=1","title":{"type":"text","content":"WebResult"},"updated":"2015-01-15T14:35:57Z","content":{"m:properties":{"d:Url":{"content":"http://www.youtube.com/watch?v=nZfkY7b5vIo","m:type":"Edm.String"},"d:DisplayUrl":{"content":"www.youtube.com/watch?v=nZfkY7b5vIo","m:type":"Edm.String"},"d:Title":{"content":"OMONOIA Vs ANORTHOSI 3-2 - YouTube","m:type":"Edm.String"},"d:Description":{"content":"OMONOIA Vs ANORTHOSI 3-2 - YouTube ... YouTube home","m:type":"Edm.String"},"d:ID":{"content":"a8870310-3a66-493e-9155-3608501305d2","m:type":"Edm.Guid"}},"type":"application/xml"}},{"id":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=' what is omonoia'&$skip=4&$top=1","title":{"type":"text","content":"WebResult"},"updated":"2015-01-15T14:35:57Z","content":{"m:properties":{"d:Url":{"content":"http://www.youtube.com/watch?v=EOg6rMweu38","m:type":"Edm.String"},"d:DisplayUrl":{"content":"www.youtube.com/watch?v=EOg6rMweu38","m:type":"Edm.String"},"d:Title":{"content":"OMONOIA Vs APOLLON 2-4 - YouTube","m:type":"Edm.String"},"
我从这个问题中得到了一些源代码:Parsing external XML to JSON in Java?
【问题讨论】: