【发布时间】:2026-01-15 04:30:01
【问题描述】:
我正在做一个项目,我只是从 API 获取一些 JSON 数据并将其显示在界面中。但是,当我尝试解析 JSON 并使用 for 循环将其放入数组时,它会将数据变为 null。
当我放入第一个 String 数组时,很好,但是每次我放入另一个时,所有其他数组都会被 null 填充
我不明白为什么会这样,可能与我解析 JSON 的方式有关?
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
public class HTTP {
private static final String USER_AGENT = "Mozilla/5.0";
private static final String AUTHENTICATION = "OrpLanM2WUiEppeeg6CW";
public static int index;
public static String output = "";
String[] result;
public static String[][] resultArray;
public static JSONArray docs;
public static void parseJSON(Object file) {
// In java JSONObject is used to create JSON object
JSONObject json = (JSONObject) file;
System.out.println(json);
docs = (JSONArray) json.get("docs");
System.out.println(docs);
if (index <= -1){
System.out.println(docs.size());
for (int i = 0; i < docs.size(); i++) {
JSONObject o = (JSONObject) docs.get(i);
String objString = o.toString();
String[] result = objString.split("[:,{}]");
System.out.println("result :"+Arrays.toString(result));
String[][] resultArray = new String[docs.size()][result.length];
for(int j = 0; j< result.length; j++){
resultArray[i][j] = result[j];
}
//resultArray[i] = result;
System.out.println(Arrays.toString(resultArray[i]));
HTTP.resultArray = resultArray;
//System.out.println(Arrays.toString(HTTP.resultArray[i]));
if(i>0) {
System.out.println(Arrays.toString(resultArray[i-1]));
//System.out.println(Arrays.toString(HTTP.resultArray[i-1]));
}
}
System.out.println(Arrays.toString(HTTP.resultArray[0]));
}
else {
JSONObject o = (JSONObject) docs.get(index);
String objString = o.toString();
String[] result = objString.split("[:,{}]");
System.out.println("result :"+result);
String[][] resultArray = new String[docs.size()][result.length];
resultArray[index] = result;
HTTP.resultArray = resultArray;
}
}
public static void sendGET(String url) throws IOException {
URL obj = new URL("https://the-one-api.dev/v2/"+url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Authorization", "Bearer " + AUTHENTICATION);
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
output = response.toString();
Object file = JSONValue.parse(output);
parseJSON(file);
} else {
System.out.println("GET request not worked");
}
}
}
【问题讨论】:
-
改造可以帮到你
-
这可以编译吗?
org.json.JSONArray没有size()方法。还有什么是 JSONValue。请添加您的导入 -
它编译
-
您对静态字段的使用是一个等待发生的意外。考虑一下如果要并行解析多个 JSON 文件会发生什么。
标签: java arrays json multidimensional-array