Android - 在数组中创建一个 JSON 数组

Android - Creating a JSON Array in an array

我正在尝试创建这样的数组:

[
  [
    {
        "id": 1,
        "value": "400"
    },
    {
        "id": 2,
        "value": "200"
    }
  ],
  [
    {
        "id": 1,
        "value": "200"
    },
    {
        "id": 2,
        "value": "100"
    }
  ]
]

我觉得这是我的 JSON 逻辑的一个简单问题,但我在某处弄错了。

这是我的代码:

    JSONArray outer = new JSONArray();
    JSONArray orows = new JSONArray();
    JSONArray inner = new JSONArray();
    JSONArray trows = new JSONArray();

    for (int i = 0; i < rows; i++) {
        String temprow = 10 + "" + qid + "" + i;
        int rid = Integer.parseInt(temprow);

        final TableRow tr = new TableRow(context);
        tr.setId(rid);
        tr.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT, 1f));

        for (int cols = 0; cols < columnHeaders.size(); cols++) {

            int cid = Integer.parseInt(tempcol);

            // Create a TextView to house the name of the province
            final EditText labelTV = new EditText(context);
            labelTV.setId(cid);

            if (answers.isEmpty()) {
                System.out.println("TABLES: Answer Empty");
                labelTV.setText(columnHeaders.get(cols));
            } else {
                JSONObject test = new JSONObject();
                for (int z = 0; z < answers.size(); z++) {
                    String ans = answers.get(z);
                    String[] parts = ans.split("<:>");
                    int col = Integer.parseInt(parts[0]);
                    int row = Integer.parseInt(parts[1]);
                    String text = parts[2];

                    if (labelTV.getId() == col && tr.getId() == row) {
                        labelTV.setText(text);
                        try {
                            test.put("id", col);
                            test.put("value", text);
                            trows.put(test);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        inner.put(trows);
        orows.put(inner);
    }
    outer.put(trows);
    System.out.println("JSON outer IS: " + outer.toString());
}

我得到的输出是:

JSON outer IS: [[{"id":206780,"value":"p1"},{"id":206781,"value":"n1"},
{"id":206782,"value":"d1"},{"id":206780,"value":"p2"},
{"id":206781,"value":"n2"},{"id":206782,"value":"d2"},
{"id":206780,"value":"p3"},{"id":206781,"value":"n3"},
{"id":206782,"value":"d3"},{"id":206780,"value":"p4"},      
{"id":206781,"value":"n4"},{"id":206782,"value":"d4"},
{"id":206780,"value":"p5"},{"id":206781,"value":"n5"},
{"id":206782,"value":"d5"}]]

我不确定我哪里出错了。

解决方案。感谢 ci_

    JSONArray inner = new JSONArray();
    for (int i = 0; i < rows; i++) {
        JSONArray trows = new JSONArray();

        String temprow = 10 + "" + qid + "" + i;
        int rid = Integer.parseInt(temprow);
        // Create a TableRow and give it an ID
        final TableRow tr = new TableRow(context);
        tr.setId(rid);
        tr.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT, 1f));

        //TODO: create the JSON object here

        for (int cols = 0; cols < columnHeaders.size(); cols++) {

            String tempcol = 20 + "" + qid + "" + cols;
            int cid = Integer.parseInt(tempcol);

            // Create a TextView to house the name of the province
            final EditText labelTV = new EditText(context);
            labelTV.setId(cid);

            if (answers.isEmpty()) {
                System.out.println("TABLES: Answer Empty");
                labelTV.setText(columnHeaders.get(cols));
            } else {
                JSONObject test = new JSONObject();
                for (int z = 0; z < answers.size(); z++) {
                    String ans = answers.get(z);
                    String[] parts = ans.split("<:>");
                    int col = Integer.parseInt(parts[0]);
                    int row = Integer.parseInt(parts[1]);
                    String text = parts[2];

                    //set the answer from the prefilled database
                    if (labelTV.getId() == col && tr.getId() == row) {
                        labelTV.setText(text);
                        try {
                            test.put("id", col);
                            test.put("value", text);
                            trows.put(test);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        inner.put(trows);
    }

您需要在外部 for 循环中初始化 trows,然后 inner 将是您的最终输出。

由于您目前正在循环外初始化 trows,因此在循环迭代之间永远不会得到 "reset",而您的最终 outer.put(trows) 只是一个包含单个元素 [=10] 的数组=].