如何循环遍历保存在 JSONObject 中的数据数组,并将其放入 JSONArray 中?

huangapple go评论114阅读模式
英文:

How to loop over an array of data that saved to a JSONObject and put it in a JSONArray?

问题

以下是翻译好的部分:

我在我的HTML中有一些字段,比如说有2行字段,我在控制器上对它们进行循环。

我希望结果是这样的:

  1. {
  2. "specifications": [
  3. {
  4. "name": "height",
  5. "value": "cm"
  6. },
  7. {
  8. "name": "weight",
  9. "value": "kg"
  10. }
  11. ]
  12. }

但是每次我使用下面的代码循环时,我只能得到迭代的最后一行保存到我的JSONArray中:

  1. JSONObject itemTypeObj = new JSONObject();
  2. JSONArray itemTypeArray = new JSONArray();
  3. JSONObject itemTypeSpecs = new JSONObject();
  4. ArrayList<String> values = new ArrayList();
  5. for (int x = 0; x < specName.length; x++) {
  6. itemTypeSpecs.put("specName", specName[x]);
  7. if (specValue[x].contains(",")) {
  8. for (String v : specValue[x].split(",")) {
  9. values.add(v.trim());
  10. }
  11. itemTypeSpecs.put("specValue", values);
  12. } else {
  13. itemTypeSpecs.put("specValue", specValue[x]);
  14. }
  15. values.clear();
  16. itemTypeArray.put(itemTypeSpecs);
  17. }
  18. itemTypeObj.put("specifications", itemTypeArray);

结果变成了这样,而不是我上面期望的样子:

  1. {
  2. "specifications": [
  3. {
  4. "name": "Weight",
  5. "value": "kg"
  6. },
  7. {
  8. "name": "Weight",
  9. "value": "kg"
  10. }
  11. ]
  12. }

我找不到为什么只获取到最后一行的原因。非常感谢您的帮助。谢谢。

英文:

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:

  1. {
  2. &quot;specifications&quot;:
  3. [
  4. {
  5. &quot;name&quot; : &quot;height&quot;,
  6. &quot;value&quot; : &quot;cm&quot;
  7. },
  8. {
  9. &quot;name&quot; : &quot;weight&quot;,
  10. &quot;value&quot; : &quot;kg&quot;
  11. }
  12. ]
  13. }

But every time I loop using my code below, I only get the last row of the iteration saved to my JSONArray

  1. JSONObject itemTypeObj = new JSONObject();
  2. JSONArray itemTypeArray = new JSONArray();
  3. JSONObject itemTypeSpecs = new JSONObject();
  4. ArrayList&lt;String&gt; values = new ArrayList();
  5. for(int x = 0; x &lt; specName.length; x++){
  6. itemTypeSpecs.put(&quot;specName&quot;, specName[x]);
  7. if (specValue[x].contains(&quot;,&quot;)) {
  8. for (String v : specValue[x].split(&quot;,&quot;)) {
  9. values.add(v.trim());
  10. }
  11. itemTypeSpecs.put(&quot;specValue&quot;, values);
  12. } else {
  13. itemTypeSpecs.put(&quot;specValue&quot;, specValue[x]);
  14. }
  15. values.clear();
  16. itemTypeArray.put(itemTypeSpecs);
  17. }
  18. itemTypeObj.put(&quot;specifications&quot;, itemTypeArray);

Result becomes like this instead of the one I expect from above

  1. {&quot;specifications&quot;:
  2. [
  3. {
  4. &quot;name&quot;:&quot;Weight&quot;,
  5. &quot;value&quot;:&quot;kg&quot;
  6. },
  7. {
  8. &quot;name&quot;:&quot;Weight&quot;,
  9. &quot;value&quot;:&quot;kg&quot;
  10. }
  11. ]
  12. }

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。因为你总是在改变相同的对象。你可以尝试这样做:

  1. JSONObject itemTypeObj = new JSONObject();
  2. JSONArray itemTypeArray = new JSONArray();
  3. for (int x = 0; x < specName.length; x++) {
  4. List<String> values = new ArrayList();
  5. JSONObject itemTypeSpecs = new JSONObject();
  6. itemTypeSpecs.put("specName", specName[x]);
  7. if (specValue[x].contains(",")) {
  8. for (String v : specValue[x].split(","))
  9. values.add(v.trim());
  10. itemTypeSpecs.put("specValue", values);
  11. } else {
  12. itemTypeSpecs.put("specValue", specValue[x]);
  13. }
  14. itemTypeArray.put(itemTypeSpecs);
  15. }
  16. 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?

  1. JSONObject itemTypeObj = new JSONObject();
  2. JSONArray itemTypeArray = new JSONArray();
  3. for (int x = 0; x &lt; specName.length; x++) {
  4. List&lt;String&gt; values = new ArrayList();
  5. JSONObject itemTypeSpecs = new JSONObject();
  6. itemTypeSpecs.put(&quot;specName&quot;, specName[x]);
  7. if (specValue[x].contains(&quot;,&quot;)) {
  8. for (String v : specValue[x].split(&quot;,&quot;))
  9. values.add(v.trim());
  10. itemTypeSpecs.put(&quot;specValue&quot;, values);
  11. } else {
  12. itemTypeSpecs.put(&quot;specValue&quot;, specValue[x]);
  13. }
  14. itemTypeArray.put(itemTypeSpecs);
  15. }
  16. itemTypeObj.put(&quot;specifications&quot;, itemTypeArray);

huangapple
  • 本文由 发表于 2020年5月29日 18:08:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/62083511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定