如何从java字符串for循环创建json数组

时间:2017-02-20 21:28:07

标签: java arrays json for-loop

我有一个JAVA for循环打印输出,如下所示。

inside for loop::{"state":"tess","path":"/content/projectpath/en/men"} inside for loop::{"state":"hello","path":"/content/projectpath/en/women"}

我的任何代码段如下所示。

for (Value val : values) {
                    //jsonobj = new JSONObject(val.getString());
                    out.println("inside for loop::" + val.getString());
                  // JSONArray jsonarr = val.getString();
               }// out.println("::"+jsonobj.toString());

如何在for循环之后获取一个JSON数组,其值应为{"state":"tess","path":"/content/projectpath/en/men"}{"state":"hello","path":"/content/projectpath/en/women"}

1 个答案:

答案 0 :(得分:3)

像这样创建一个JSONArray。在循环对象上插入循环对象。最后你会得到一个数组,请尝试一下。

JSONArray list = new JSONArray();
list.add(val);