英文:
How to create a non-static list from JSON parse in java?
问题
以下是翻译好的内容:
我对Java还比较新,遇到了我目前正在处理的项目中的一个问题。我正在尝试通过HTTP请求从联合国贸易数据数据库(UN Comtrade)获取数据,然后将其解析到一个ArrayList中。
我已经使用下面的代码做到了这一点,但接下来我想在这个类中创建一个额外的'getList'方法,以便我可以从程序的其他部分调用该列表。
然而,我无法弄清楚如何做到这一点,因为解析方法是静态的,所以它创建的列表是不可访问的。有谁能帮帮我吗?
我的主要类:
public class Main {
public static void main(String[] args) {
ReporterArea reporterArea = new ReporterArea();
reporterArea.fetchReporterArea();
}
}
报告员地区类:
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpClient;
import java.net.URI;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
public class ReporterArea {
public void fetchReporterArea(){
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://comtrade.un.org/api/get?max=500&type=C&freq=A&px=HS&ps=2016&r=all&p=0&rg=2&cc=TOTAL")).build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(ReporterArea::parse)
.join();
}
public static String parse(String dataset){
JSONObject countries = new JSONObject(dataset);
ArrayList<String> list = new ArrayList<String>();
JSONArray array = countries.getJSONArray("dataset");
for (int i = 0; i < array.length(); i++){
String combined = array.getJSONObject(i).optString("rtTitle") + "; " + array.getJSONObject(i).optString("rtCode");
list.add(combined);
}
for (String i : list){
System.out.println(i);
}
return null;
}
}
英文:
I'm still relatively new to Java and have hit a problem in the project I'm currently working on. I'm trying to get data from UN Comtrade via an HTTP request and then to parse this into an ArrayList.
I've managed to do that with the code below, but what I would then like to do is create an additional 'getList' method in this class so that I can call the list from other parts of the program.
However, I can't figure out how to do this as the parse method is static and so the list that it creates is unaccessible. Can anyone help me out with this?
My main class:
public class Main {
public static void main(String[] args) {
ReporterArea reporterArea = new ReporterArea();
reporterArea.fetchReporterArea();
}
The reporter area class:
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpClient;
import java.net.URI;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
public class ReporterArea {
public void fetchReporterArea(){
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://comtrade.un.org/api/get?max=500&type=C&freq=A&px=HS&ps=2016&r=all&p=0&rg=2&cc=TOTAL")).build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(ReporterArea::parse)
.join();
}
public static String parse(String dataset){
JSONObject countries = new JSONObject(dataset);
ArrayList<String> list = new ArrayList<String>();
JSONArray array = countries.getJSONArray("dataset");
for (int i = 0; i < array.length(); i++){
String combined = array.getJSONObject(i).optString("rtTitle") + "; " + array.getJSONObject(i).optString("rtCode");
list.add(combined);
}
for (String i : list){
System.out.println(i);
}
return null;
}
}
答案1
得分: 1
如果您不想更改 API 逻辑,只需创建一个私有的 List<String> myList 变量,并且在添加元素到列表的静态方法的循环块之后,编写以下代码:
private static List<String> myList; // 在 OP 的评论后添加了 static 关键字
for (int i = 0; i < array.length(); i++){
String combined = array.getJSONObject(i).optString("rtTitle") + "; " + array.getJSONObject(i).optString("rtCode");
list.add(combined);
}
myList = new ArrayList<>(list);
之后,您可以创建一个公共的 getter 方法,如下所示:
public List<String> getList(){
if(myList !=null) {
return myList;
} else {
return new ArrayList<>();
}
}
您必须小心,并确保在解析之后调用 getList() 方法。
英文:
If you don't want to change the api logic you can just create a private List<String> myList variable and after the for block that adds elements to the list in the static method, write
private static List<String> myList; //edit added static keyword after OP's comment
for (int i = 0; i < array.length(); i++){
String combined = array.getJSONObject(i).optString("rtTitle") + "; " + array.getJSONObject(i).optString("rtCode");
list.add(combined);
}
myList = new ArrayList<>(list);
After that you can create a public getter method like this:
public List<String> getList(){
if(myList !=null) {
return myList;
} else {
return new ArrayList<>();
}
}
you have to be careful and be sure that the getList() method is called after the parse
答案2
得分: 0
如果您需要获取解析后的字符串列表,只需从parse
方法中返回该列表即可。此方法可以是静态的,因为它不依赖于任何实例数据,正如上面提到的那样。
然后,如果您确实需要异步检索数据,您可能还希望重构fetchReporterArea
方法以返回CompletableFuture<List<String>>
类型。
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpClient;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.json.JSONArray;
import org.json.JSONObject;
public class ReporterArea {
public CompletableFuture<List<String>> fetchReporterArea() {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(
"https://comtrade.un.org/api/get?max=500&type=C&freq=A&px=HS&ps=2016&r=all&p=0&rg=2&cc=TOTAL"))
.build();
return client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(ReporterArea::parse);
}
public static List<String> parse(String dataset) {
JSONObject countries = new JSONObject(dataset);
List<String> list = new ArrayList<String>();
JSONArray array = countries.getJSONArray("dataset");
for (int i = 0; i < array.length(); i++) {
JSONObject row = array.getJSONObject(i);
list.add(row.optString("rtTitle") + "; " + row.optString("rtCode"));
}
return list;
}
}
然后,main
方法应更新如下:
public static void main(String[] args) throws InterruptedException, ExecutionException {
ReporterArea reporterArea = new ReporterArea();
CompletableFuture<List<String>> asyncResult = reporterArea.fetchReporterArea();
asyncResult.get().forEach(System.out::println);
}
输出(缩写)
阿尔巴尼亚; 8
阿尔及利亚; 12
安道尔; 20
安哥拉; 24
...
英文:
If you need to get the list of the parsed strings you should just return it from the parse
method, and it can be static because it does not depend on any instance data as mentioned above.
Then you may also want to refactor fetchReporterArea
to return CompletableFuture<List<String>>
if you really need to retrieve the data asynchronously.
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpClient;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.json.JSONArray;
import org.json.JSONObject;
public class ReporterArea {
public CompletableFuture<List<String>> fetchReporterArea() {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(
"https://comtrade.un.org/api/get?max=500&type=C&freq=A&px=HS&ps=2016&r=all&p=0&rg=2&cc=TOTAL"))
.build();
return client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(ReporterArea::parse);
}
public static List<String> parse(String dataset) {
JSONObject countries = new JSONObject(dataset);
List<String> list = new ArrayList<String>();
JSONArray array = countries.getJSONArray("dataset");
for (int i = 0; i < array.length(); i++) {
JSONObject row = array.getJSONObject(i);
list.add(row.optString("rtTitle") + "; " + row.optString("rtCode"));
}
return list;
}
}
Then method main
should be updated as follows:
public static void main(String[] args) throws InterruptedException, ExecutionException {
ReporterArea reporterArea = new ReporterArea();
CompletableFuture<List<String>> asyncResult = reporterArea.fetchReporterArea();
asyncResult.get().forEach(System.out::println);
}
Output (abbreviated)
Albania; 8
Algeria; 12
Andorra; 20
Angola; 24
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论