Java 如何通过for循环创建多个JSON对象
我需要根据数据库查询的结果集创建数量可变的JSON对象和JSON数组。JSON格式与下面用于google图表的格式非常相似Java 如何通过for循环创建多个JSON对象,java,json,google-visualization,Java,Json,Google Visualization,我需要根据数据库查询的结果集创建数量可变的JSON对象和JSON数组。JSON格式与下面用于google图表的格式非常相似 { “cols”: [ {"id":"","label":"year","type":"string"}, {"id":"","label":"sales","type":"number"}, {"id":"","label":"expenses","type":"number"} ], “rows”: [ {"c":[{"v":"2001"},{"v":3},{"v":5
{
“cols”: [
{"id":"","label":"year","type":"string"},
{"id":"","label":"sales","type":"number"},
{"id":"","label":"expenses","type":"number"}
],
“rows”: [
{"c":[{"v":"2001"},{"v":3},{"v":5}]},
{“c”:[{"v":"2002"},{"v":5},{"v":10}]},
{“c”:[{"v":"2003"},{"v":6},{"v":4}]},
{“c”:[{"v":"2004"},{"v":8},{"v":32}]},
{“c”:[{"v":"2005"},{"v":3},{"v":56}]}
]
}
我的问题是,我觉得这应该是一个简单的答案,如何在for循环中创建多个具有唯一名称的JSON对象?我的尝试:
for(int i=0;i<10;i++) {
JSONObject "tempName"+i = new JSONObject();
}
for(int i=0;i不能动态构造Java变量名。
我不知道怎么还没有人回答这个问题,但给你
JSONObject objects = new JSONObject[10];
for(int i = 0 ; i < objects.length ; i++) {
objects[i] = new JSONObject();
}
JSONObject o = objects[2]; // get the third one
JSONArray arr=new JSONArray();
HashMap=newHashMap();
对于(int i=0;i<10;i++){
JSONObject json=新的JSONObject();
json.put(“id”,i);
json.put(“名字”、“abc”+i);
map.put(“json”+i,json);
arr.put(map.get(“json”+i));
}
println(“json字符串是”+arr.toString());
输出为
json字符串是
[
{“id”:0,“firstName”:“abc0”},
{“id”:1,“firstName”:“abc1”},
{“id”:2,“firstName”:“abc2”},
{“id”:3,“firstName”:“abc3”},
{“id”:4,“firstName”:“abc4”}
]
导入org.json.JSONArray;
导入org.json.JSONObject;
导入java.io.File;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.Writer;
导入java.util.array;
导入java.util.List;
/**
*将不同文件夹中的文件写入JSON的一些解决方案
*@作者Dmytro Melnychuk
*/
公共类ParseFilesToJson{
公共静态void main(字符串[]args){
列表文件夹名称=数组.asList(“dwg”、“eta en”、“eta pl”、“inst”、“prod”、“tds en”、“tds pl”);
folderNames.forEach(it->{
写入文件(it);
});
}
私有静态void writeIntoFile(字符串folderName){
文件目录=新文件(“C:\\Users\\mel\\AppData\\Roaming\\data\\\”+folderName);
File[]directories=directory.listFiles();
JSONArray数组=新的JSONArray();
JSONObject json;
对于(int i=0;i
解决方案已为Java 7及以下版本的用户准备好:)使用数组、列表或地图。不知道我怎么会没有想到这一点,谢谢。太好了!做得好。
Map<String, JSONObject> map = new HashMap<>();
for(int i = 0 ; i < 10 ; i++) {
map.put("tempName" + i, new JSONObject());
}
JSONObject o = map.get("tempName3"); // get the 4th created (hashmaps don't have an ordering though)
JSONArray arr = new JSONArray();
HashMap<String, JSONObject> map = new HashMap<String, JSONObject>();
for(int i = 0 ; i < 10 ; i++) {
JSONObject json=new JSONObject();
json.put("id",i);
json.put("firstName","abc"+i);
map.put("json" + i, json);
arr.put(map.get("json" + i));
}
System.println("The json string is " + arr.toString());
OutPut is
The json string is
[
{"id":0,"firstName":"abc0"},
{"id":1,"firstName":"abc1"},
{"id":2,"firstName":"abc2"},
{"id":3,"firstName":"abc3"},
{"id":4,"firstName":"abc4"}
]
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;
/**
* Some solution for write files from different folders to JSON
* @author Dmytro Melnychuk
*/
public class ParseFilesToJson {
public static void main(String[] args) {
List<String> folderNames = Arrays.asList("dwg", "eta-en", "eta-pl", "inst", "prod", "tds-en", "tds-pl");
folderNames.forEach(it -> {
writeIntoFile(it);
});
}
private static void writeIntoFile(String folderName) {
File directory = new File("C:\\Users\\mel\\AppData\\Roaming\\data\\" + folderName);
File[] directories = directory.listFiles();
JSONArray array = new JSONArray();
JSONObject json;
for (int i = 0; i < directories.length; i++) {
json = new JSONObject();
json.put("name", directories[i].getName());
json.put("version", 1);
array.put(json);
}
try (Writer file = new FileWriter("d:\\" + folderName + ".json")) {
array.write(file, 2, 0);
} catch (IOException e) {
}
}
}