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.
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_
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);
}
trowsinside your outerforloop, and theninnershould be your final output. I can't test this right now, but it seems worth a shot.