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

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

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:

{
&quot;specifications&quot;:
[
    {
      &quot;name&quot; : &quot;height&quot;,
      &quot;value&quot; : &quot;cm&quot;
    },
    {
      &quot;name&quot; : &quot;weight&quot;,
      &quot;value&quot; : &quot;kg&quot;
    }
  ]
}

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&lt;String&gt; values = new ArrayList();

        for(int x = 0; x &lt; specName.length; x++){

            itemTypeSpecs.put(&quot;specName&quot;, specName[x]);

            if (specValue[x].contains(&quot;,&quot;)) {

                for (String v : specValue[x].split(&quot;,&quot;)) {
                    values.add(v.trim());
                }

                itemTypeSpecs.put(&quot;specValue&quot;, values);
            } else {
                itemTypeSpecs.put(&quot;specValue&quot;, specValue[x]);
            }

            values.clear();

            itemTypeArray.put(itemTypeSpecs);

        }

itemTypeObj.put(&quot;specifications&quot;, itemTypeArray);

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

{&quot;specifications&quot;:
  [
   {
    &quot;name&quot;:&quot;Weight&quot;,
    &quot;value&quot;:&quot;kg&quot;
   },
   {
    &quot;name&quot;:&quot;Weight&quot;,
    &quot;value&quot;:&quot;kg&quot;
   }
  ]
}

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 &lt; specName.length; x++) {
            List&lt;String&gt; values = new ArrayList();
            JSONObject itemTypeSpecs = new JSONObject();
            itemTypeSpecs.put(&quot;specName&quot;, specName[x]);
            
            if (specValue[x].contains(&quot;,&quot;)) {
                for (String v : specValue[x].split(&quot;,&quot;))
                    values.add(v.trim());
                itemTypeSpecs.put(&quot;specValue&quot;, values);
            } else {
                itemTypeSpecs.put(&quot;specValue&quot;, specValue[x]);
            }
            itemTypeArray.put(itemTypeSpecs);
        }
        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:

确定