【问题标题】:Read json data from url using java使用java从url读取json数据
【发布时间】:2021-03-23 14:33:45
【问题描述】:

我读过一些类似的答案

Simplest way to read json from a URL in java

但它不能工作。

好的,这是我的代码

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

}

我想要一个可以在 Eclipse 中运行的示例。 我已经安装了 gson,我使用的是 jdk11。

这是我的网址:https://ws.yunlin.gov.tw/001/Upload/539/opendata/15369/1096/61b84dbc-5fe5-4569-8b94-4eebf1deef71.json

我想把它读入java

非常感谢

我想我已经将 org.json 添加到路径中,但仍然有错误

【问题讨论】:

  • "我想要一个可以复制并在 Eclipse 中运行的示例。" - 你可能不会得到那个。我们很乐意提供帮助,但除非您付出一些努力,例如通过自己尝试,您的问题比得到答案更有可能得到解决。 “他们都不能工作!” - 请详细说明为什么?你遇到了什么问题?
  • ok.thx 会根据需要显示我的代码。我的努力是阅读该主题几个小时,并且知道将 gson 添加到路径中,也许我应该粘贴我阅读的所有材料?
  • 不,您不需要展示您已阅读的所有内容,我们也不需要知道您在该主题上工作了多长时间。但我们需要看到您至少尝试了一些东西,尝试做一些研究并理解您所阅读的内容(如果您无法理解某些内容,请说明它以便我们可以提供帮助并知道如何更好地提供帮助)。
  • 您发布的错误已经说明了您的问题:您的代码无法编译,因为JSONObjectJSONParser 等无法解决。由于您的图像显示了一个类 MainGet 并且您发布的代码将类命名为 hello 我们只能假设您没有显示您的实际代码,因此我们不得不猜测:您没有导入所有需要的类或您没有将正确的库放在类路径上(或不正确)。请注意,org.jsonnet.sf.json 似乎是不同的库。
  • 修改了我的代码

标签: java json api io


【解决方案1】:

您没有提供错误描述。 假设它是 JSON 解析错误。 该 URL 不返回 JSONObject,而是返回 JSONArray。

这几乎是复制和粘贴解决方案。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2013-10-04
    • 2016-10-26
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多