简体   繁体   English

Android - 在数组中创建 JSON 数组

[英]Android - Creating a JSON Array in an array

I'm trying to create an array like this:我正在尝试创建一个这样的数组:

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

I have a feeling that it's a simple problem with my JSON logic but i'm getting it wrong somewhere.我有一种感觉,这是我的 JSON 逻辑的一个简单问题,但我在某处弄错了。

Here is my code:这是我的代码:

    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());
}

The output that i'm getting is:我得到的输出是:

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"}]]

I'm not sure where I am going wrong.我不确定我哪里出错了。

SOLUTION.解决方案。 Thanks to ci_感谢 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);
    }

You need to initialize trows inside your outer for loop, and then inner would be your final output.您需要在外部 for 循环内初始化trows ,然后inner将是您的最终输出。

Since you are currently initializing trows outside the loop it never gets "reset" between loop iterations, and your final outer.put(trows) is just an array with a single element of trows .由于您当前正在循环外部初始化trows ,因此在循环迭代之间永远不会“重置”,并且您的最终outer.put(trows)只是一个具有trows单个元素的trows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM