【问题标题】:Why is the data in my array of string arrays turning into null?为什么我的字符串数组数组中的数据变为空?
【发布时间】: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");
        }
    }
}

【问题讨论】:

  • 你能显示minimal reproducible example吗?
  • 改造可以帮到你
  • 这可以编译吗? org.json.JSONArray 没有 size() 方法。还有什么是 JSONValue。请添加您的导入
  • 它编译
  • 您对静态字段的使用是一个等待发生的意外。考虑一下如果要并行解析多个 JSON 文件会发生什么。

标签: java arrays json multidimensional-array


【解决方案1】:

问题在于,您不仅在每个循环周期中重新创建数组,甚至没有使用上面的静态变量。

String[][] resultArray = new String[docs.size()][result.length];

通过这样做,您删除了它的值。我认为您的问题是您不知道此时阵列需要多大。我不知道这是否是您需要的,但您可以尝试这样做:

(我会假装你想使用你的静态变量)

你的数组在这里:

public static String[][] resultArray;

然后你像这样初始化它:

  • 你有你的docs.size() 就像上面一样
  • 第二个值只是一些随机占位符
  • (注意:你初始化数组一次,它不能在循环中,否则你会得到和你一样的结果。你可以在打印docs.size()之后马上做)
resultArray = new String[docs.size()][10];

然后在你的循环中,你这样做:

resultArray[i] = result;

如果这样做,不仅不应删除您的数据,而且您也不需要将数据从结果数组 (result) 复制到实际数组 (resultArray) 的循环


另外,在你的 else 块中,你只需在行中省略 String[][]

String[][] resultArray = new String[docs.size()][result.length];

通过这样做,您应该能够从上面调用您的静态变量,而不是创建一个新变量。

【讨论】: