Search code examples
javajsoup

Fetching data from tags using jsoup


So i try to fetch the data from pre tag, i set doc connect to url select pre tag and the result gone wrong, the data i need to fetch press 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());
  }

Solution

  • This works for me:

    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());
            }
        }
    }
    

    But you'll want to be able to parse the json you get back using a Json parser. There are many to choose from. Gson, Jackson, Jayway, etc. You'll have to pick what's right for you.

    Output:

    {
      "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