我试图创建一个JSONArray
具有JSONObjects
使用简单的Java程序如下格式打印
[{"url":"http://www.google.com"},{"url":"http://www.yahoo.com"}]
Java程序
JSONObject object = new JSONObject();
String[] s ={"http://www.google.com","http://www.yahoo.com"}
for(String str : s)
{
object.put("url",str);
System.out.println(object);
}
但实际输出是,我不知道我在这里做了什么错。
{"url":"http://www.google.com"}{"url":"http://www.yahoo.com"}
发布于 2019-07-22 15:47:13
输出似乎JSONArray
有两个JSONObject
[{"url":"http://www.google.com"},{"url":"http://www.yahoo.com"}]
所以首先JSONArray
在循环外创建,然后迭代字符串数组,JSONObject
最后创建将其添加到JSONArray
JSONArray array = new JSONArray();
for(String str : s) {
JSONObject obj1 = new JSONObject();
obj1.put("url",str);
array.put(obj1);
}
System.out.println(array);
https://stackoverflow.com/questions/-100007080
复制相似问题