0

i haved read some answers like

Simplest way to read json from a URL in java

but it can not work.

ok,here is my code

package day1;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class MainGet {

    public static void main(String[] args) {
        try {

            URL url = new URL("https://api.covid19api.com/summary");

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.connect();

            //Getting the response code
            int responsecode = conn.getResponseCode();

            if (responsecode != 200) {
                throw new RuntimeException("HttpResponseCode: " + responsecode);
            } else {

                String inline = "";
                Scanner scanner = new Scanner(url.openStream());

                //Write all the JSON data into a string using a scanner
                while (scanner.hasNext()) {
                    inline += scanner.nextLine();
                }

                //Close the scanner
                scanner.close();

                //Using the JSON simple library parse the string into a json object
                JSONParser parse = new JSONParser();
                JSONObject data_obj = (JSONObject) parse.parse(inline);

                //Get the required object from the above created object
                JSONObject obj = (JSONObject) data_obj.get("Global");

                //Get the required data using its key
                System.out.println(obj.get("TotalRecovered"));

                JSONArray arr = (JSONArray) data_obj.get("Countries");

                for (int i = 0; i < arr.size(); i++) {

                    JSONObject new_obj = (JSONObject) arr.get(i);

                    if (new_obj.get("Slug").equals("albania")) {
                        System.out.println("Total Recovered: " + new_obj.get("TotalRecovered"));
                        break;
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

i want a example that can run in eclipse. i haved install gson and i use jdk11.

here is my url :https://ws.yunlin.gov.tw/001/Upload/539/opendata/15369/1096/61b84dbc-5fe5-4569-8b94-4eebf1deef71.json

i want read it into java

thanks so much

enter image description here

i think i have added org.json into path,but still haved error

陳寧寬
  • 33
  • 8
  • 1
    "I want a example that can just copy it and run in eclipse." - you're probably not going to get that. We're glad to help but unless you show some effort, e.g. by trying it yourself, your question is more likely to get closed than getting answers. "none of them can work!" - care to elaborate why? What problems did you run into? – Thomas Mar 23 '21 at 14:39
  • ok.thx will show my code as you want.my effort is reading the topic several hours,and know add gson into path maybe i should paste all the material i read? – 陳寧寬 Mar 23 '21 at 15:05
  • 1
    No, you don't need to show everything you've read and we don't need to know how long you've been working on the topic. But we need to see that you at least tried something, tried to do some research and understand what you've read (and if you fail to understand something then state it so we can help and know how to help better). – Thomas Mar 23 '21 at 15:28
  • 1
    The error you've posted already states your problem: your code doesn't compile because `JSONObject`, `JSONParser` etc. cannot be resolved. Since your image shows a class `MainGet` and your posted code names the class `hello` we can only assume you didn't show your actual code and thus we'd have to guess: you didn't import all the needed classes or you didn't put the correct libraries on the classpath (or not correctly). Note that `org.json` and `net.sf.json` seem to be different libraries. – Thomas Mar 23 '21 at 15:30
  • haved modify my code – 陳寧寬 Mar 23 '21 at 15:57

1 Answers1

0

You have not provided error description. Assuming it's JSON parsing error. That URL does not return JSONObject but a JSONArray.

This is almost copy and paste solution.

https://mkyong.com/java/gson-how-to-parse-json-arrays-an-array-of-arrays/

Arfat Binkileb
  • 641
  • 4
  • 18