gpt4 book ai didi

Java - 未找到 JSONObject

转载 作者:行者123 更新时间:2023-12-01 17:08:04 25 4
gpt4 key购买 nike

我正在尝试从 API 收集单个数据,即某个国家/地区的人口。除了从 JSON 中删除人口值之外,一切正常。

{"Info":[{"area":301336,"nativeName":"Italia","capital":"Rome","demonym":"Italian","flag":"https://restcountries.eu/data/ita.svg","alpha2Code":"IT","languages":[{"nativeName":"Italiano","iso639_2":"ita","name":"Italian","iso639_1":"it"}],"borders":["AUT","FRA","SMR","SVN","CHE","VAT"],"subregion":"Southern Europe","callingCodes":["39"],"regionalBlocs":[{"otherNames":[],"acronym":"EU","name":"European Union","otherAcronyms":[]}],"gini":36,"population":60665551,"numericCode":"380","alpha3Code":"ITA","topLevelDomain":[".it"],"timezones":["UTC+01:00"],"cioc":"ITA","translations":{"br":"Itália","de":"Italien","pt":"Itália","ja":"イタリア","hr":"Italija","it":"Italia","fa":"ایتالیا","fr":"Italie","es":"Italia","nl":"Italië"},"name":"Italy","altSpellings":["IT","Italian Republic","Repubblica italiana"],"region":"Europe","latlng":[42.83333333,12.83333333],"currencies":[{"symbol":"\u20ac","code":"EUR","name":"Euro"}]}]}

在 JSON 中,它被称为“Population”。

这是我的用户输入代码

public static String UserInputsDetails() {
System.out.println("Please input the country name");
Scanner in = new Scanner(System.in);
String Input = in.nextLine();
return Input;
}

这是我的 JSON Getter 代码

    public static JSONArray MakeConnection(String countryname) {
JSONArray JSON = null;
try {

String url = "https://restcountries.eu/rest/v2/name/" + countryname;

URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

JSON = new JSONArray(response.toString());

} catch (Exception e) {
System.out.println(e);
}
return JSON;
}

这是我的结果代码,用于获取人口

    public static void PrintResult(JSONArray JSON){
String population = null;
try {
JSONObject jobj = new JSONObject();
jobj.put("Info", JSON);

population = jobj.getString("population");
System.out.println(jobj);

System.out.println(population);

} catch (Exception e) {
System.out.println(e);
}

}

最后,这是我的主要内容

public static void main(String []args) {
String Input = UserInput.UserInputsDetails();
JSONArray JSON = Connection.MakeConnection(Input);
Result.PrintResult(JSON);
}

我收到错误

org.json.JSONException: JSONObject["population"] not found.

我做错了什么?

最佳答案

删除这部分:

JSONObject jobj = new JSONObject();
jobj.put("Info", JSON);

population = jobj.getString("population");
System.out.println(jobj);

System.out.println(population);

JSON 已经是一个数组了,为什么要把它转换成 JSONObject?

更改为这样的内容:

Long population = JSON.getJSONObject(0).getLong("population"); 

关于Java - 未找到 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61446730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com