java - Android - 在数组中创建 JSON 数组

标签 java android arrays json

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

[
  [
    {
        "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);
    }

最佳答案

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

由于您当前正在初始化trows在循环之外,它永远不会在循环迭代之间“重置”,并且您的最终 outer.put(trows)只是一个包含单个元素 trows 的数组.

关于java - Android - 在数组中创建 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32400357/

相关文章:

java - 哪些 Java 版本引入了哪些语言特性?

arrays - 处理中的 SplitTokens 问题

java - 可以在 Kotlin 中使用 LocalDateTime 作为 RequestParam 吗?

java - Android:Android 应用程序中的所有 Activity 是在同一个线程中运行还是在它们自己的单独线程中运行?

android - SimpleCursorAdapter 的替代品?

java - 修复 Firebase 实时数据库问题

Android:是否有另一种方法来显示字符串数组中的数据?

c - 如何在C中将静态数组的所有元素重置为0

java - Jetty 显示 http 错误消息 503 SERVICE_UNAVAILABLE

java - 将字符串从 EditText 转换为整数会导致应用程序在启动前停止工作