如何解决使用jsoup从标签中获取数据
所以我尝试从预标签获取数据,我将doc connect设置为url选择预标签,结果出错了,我需要获取数据 here
String url="http://api.airvisual.com/v2/countries?key=9c2dd8c2-1053-43fa-9357-6d3aa876aabc";
Document doc=Jsoup.connect(url).get();
for(Element a: doc.select("pre"))
{
System.out.println(a.text());
}
解决方法
这对我有用:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
class AirVisualJsonData {
public static void main(String[] args) throws IOException {
String url = "http://api.airvisual.com/v2/countries?key=9c2dd8c2-1053-43fa-9357-6d3aa876aabc";
Document document = Jsoup.connect(url).ignoreContentType(true).get();
for (Element node : document.select("body")) {
System.out.println(node.text());
}
}
}
但是您将希望能够使用Json解析器解析返回的json。有很多选择。 Gson,Jackson,Jayway等。您必须选择最适合自己的东西。
输出:
{
"status": "success","data": [
{
"country": "Afghanistan"
},{
"country": "Algeria"
},{
"country": "Andorra"
},{
"country": "Angola"
},{
"country": "Argentina"
},{
"country": "Armenia"
},{
"country": "Australia"
},{
"country": "Austria"
},{
"country": "Bahamas"
},{
"country": "Bahrain"
},{
"country": "Bangladesh"
},{
"country": "Belgium"
},{
"country": "Bosnia Herzegovina"
},{
"country": "Brazil"
},{
"country": "Brunei"
},{
"country": "Bulgaria"
},{
"country": "Canada"
},{
"country": "Chile"
},{
"country": "China"
},{
"country": "Colombia"
},{
"country": "Croatia"
},{
"country": "Cyprus"
},{
"country": "Czech Republic"
},{
"country": "Denmark"
},{
"country": "Ecuador"
},{
"country": "Ethiopia"
},{
"country": "Finland"
},{
"country": "France"
},{
"country": "Germany"
},{
"country": "Ghana"
},{
"country": "Guatemala"
},{
"country": "Hong Kong SAR"
},{
"country": "Hungary"
},{
"country": "India"
},{
"country": "Indonesia"
},{
"country": "Iran"
},{
"country": "Iraq"
},{
"country": "Ireland"
},{
"country": "Israel"
},{
"country": "Italy"
},{
"country": "Ivory Coast"
},{
"country": "Japan"
},{
"country": "Jordan"
},{
"country": "Kazakhstan"
},{
"country": "Kosovo"
},{
"country": "Kuwait"
},{
"country": "Kyrgyzstan"
},{
"country": "Latvia"
},{
"country": "Lithuania"
},{
"country": "Luxembourg"
},{
"country": "Macao SAR"
},{
"country": "Malaysia"
},{
"country": "Malta"
},{
"country": "Mexico"
},{
"country": "Mongolia"
},{
"country": "Myanmar"
},{
"country": "Nepal"
},{
"country": "Netherlands"
},{
"country": "New Caledonia"
},{
"country": "New Zealand"
},{
"country": "Nigeria"
},{
"country": "North Macedonia"
},{
"country": "Norway"
},{
"country": "Oman"
},{
"country": "Pakistan"
},{
"country": "Peru"
},{
"country": "Philippines"
},{
"country": "Poland"
},{
"country": "Portugal"
},{
"country": "Puerto Rico"
},{
"country": "Romania"
},{
"country": "Russia"
},{
"country": "San Marino"
},{
"country": "Serbia"
},{
"country": "Singapore"
},{
"country": "Slovakia"
},{
"country": "Slovenia"
},{
"country": "South Africa"
},{
"country": "South Korea"
},{
"country": "Spain"
},{
"country": "Sri Lanka"
},{
"country": "Sweden"
},{
"country": "Switzerland"
},{
"country": "Syria"
},{
"country": "Taiwan"
},{
"country": "Thailand"
},{
"country": "Turkey"
},{
"country": "U.S. Virgin Islands"
},{
"country": "USA"
},{
"country": "Uganda"
},{
"country": "Ukraine"
},{
"country": "United Arab Emirates"
},{
"country": "United Kingdom"
},{
"country": "Uzbekistan"
},{
"country": "Vietnam"
},{
"country": "Yemen"
}
]
}
Process finished with exit code 0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。