0

我需要根据数据库查询的结果集创建可变数量的 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}]},
{“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();
}
4

3 回答 3

5

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

数组不能动态调整大小。List如果您需要这种行为,您应该使用适当的实现。如果要按名称访问元素,还可以使用Map.

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)
于 2013-09-08T20:42:01.670 回答
5
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"}
]
于 2014-01-31T07:44:51.440 回答
0
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) {
        }
    }
}

解决方案已为使用 Java 7 及以下版本的人做好准备 :)

于 2016-09-14T13:16:44.830 回答