英文:
How to loop over an array of data that saved to a JSONObject and put it in a JSONArray?
问题
以下是翻译好的部分:
我在我的HTML中有一些字段,比如说有2行字段,我在控制器上对它们进行循环。
我希望结果是这样的:
{
"specifications": [
{
"name": "height",
"value": "cm"
},
{
"name": "weight",
"value": "kg"
}
]
}
但是每次我使用下面的代码循环时,我只能得到迭代的最后一行保存到我的JSONArray中:
JSONObject itemTypeObj = new JSONObject();
JSONArray itemTypeArray = new JSONArray();
JSONObject itemTypeSpecs = new JSONObject();
ArrayList<String> values = new ArrayList();
for (int x = 0; x < specName.length; x++) {
itemTypeSpecs.put("specName", specName[x]);
if (specValue[x].contains(",")) {
for (String v : specValue[x].split(",")) {
values.add(v.trim());
}
itemTypeSpecs.put("specValue", values);
} else {
itemTypeSpecs.put("specValue", specValue[x]);
}
values.clear();
itemTypeArray.put(itemTypeSpecs);
}
itemTypeObj.put("specifications", itemTypeArray);
结果变成了这样,而不是我上面期望的样子:
{
"specifications": [
{
"name": "Weight",
"value": "kg"
},
{
"name": "Weight",
"value": "kg"
}
]
}
我找不到为什么只获取到最后一行的原因。非常感谢您的帮助。谢谢。
英文:
I have fields in my HTML, say for example 2 rows of fields which I loop over on my controller.
I want it to result to something like this:
{
"specifications":
[
{
"name" : "height",
"value" : "cm"
},
{
"name" : "weight",
"value" : "kg"
}
]
}
But every time I loop using my code below, I only get the last row of the iteration saved to my JSONArray
JSONObject itemTypeObj = new JSONObject();
JSONArray itemTypeArray = new JSONArray();
JSONObject itemTypeSpecs = new JSONObject();
ArrayList<String> values = new ArrayList();
for(int x = 0; x < specName.length; x++){
itemTypeSpecs.put("specName", specName[x]);
if (specValue[x].contains(",")) {
for (String v : specValue[x].split(",")) {
values.add(v.trim());
}
itemTypeSpecs.put("specValue", values);
} else {
itemTypeSpecs.put("specValue", specValue[x]);
}
values.clear();
itemTypeArray.put(itemTypeSpecs);
}
itemTypeObj.put("specifications", itemTypeArray);
Result becomes like this instead of the one I expect from above
{"specifications":
[
{
"name":"Weight",
"value":"kg"
},
{
"name":"Weight",
"value":"kg"
}
]
}
I can't find the reason why it's only getting the last row. Any help is appreciated. Thank you.
答案1
得分: 3
你应该在第一个“for循环”内部创建数组列表(array list)和JSONObject。因为你总是在改变相同的对象。你可以尝试这样做:
JSONObject itemTypeObj = new JSONObject();
JSONArray itemTypeArray = new JSONArray();
for (int x = 0; x < specName.length; x++) {
List<String> values = new ArrayList();
JSONObject itemTypeSpecs = new JSONObject();
itemTypeSpecs.put("specName", specName[x]);
if (specValue[x].contains(",")) {
for (String v : specValue[x].split(","))
values.add(v.trim());
itemTypeSpecs.put("specValue", values);
} else {
itemTypeSpecs.put("specValue", specValue[x]);
}
itemTypeArray.put(itemTypeSpecs);
}
itemTypeObj.put("specifications", itemTypeArray);
英文:
You should crate array list and JSONObject inside first "for loop".Because you change always same objects Can yo try this?
JSONObject itemTypeObj = new JSONObject();
JSONArray itemTypeArray = new JSONArray();
for (int x = 0; x < specName.length; x++) {
List<String> values = new ArrayList();
JSONObject itemTypeSpecs = new JSONObject();
itemTypeSpecs.put("specName", specName[x]);
if (specValue[x].contains(",")) {
for (String v : specValue[x].split(","))
values.add(v.trim());
itemTypeSpecs.put("specValue", values);
} else {
itemTypeSpecs.put("specValue", specValue[x]);
}
itemTypeArray.put(itemTypeSpecs);
}
itemTypeObj.put("specifications", itemTypeArray);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论