使用 jsoup 从标签中获取数据

Fetching data from tags using jsoup

所以我尝试从 pre 标签中获取数据,我将 doc 连接到 url select pre 标签,结果出错了,我需要获取数据按 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