Java 将JSON对象列表转换为单个JSON数组

Java 将JSON对象列表转换为单个JSON数组,java,json,Java,Json,我正在使用org.json.simple.JSONObject库读取一些文本并将其形成json 我的守则如下: 公共类性能度量{ 私有静态字符串filePath=“Shell\u Pricing\u Metrics.json”; 私有静态字符串jsoncontent; 公共静态void clearFileContents(字符串文件路径)引发IOException{ 文件f1=新文件(文件路径); 新文件编写器(f1); } 公共静态void metricAsJSON(字符串testName,

我正在使用org.json.simple.JSONObject库读取一些文本并将其形成json

我的守则如下:

公共类性能度量{
私有静态字符串filePath=“Shell\u Pricing\u Metrics.json”;
私有静态字符串jsoncontent;
公共静态void clearFileContents(字符串文件路径)引发IOException{
文件f1=新文件(文件路径);
新文件编写器(f1);
}
公共静态void metricAsJSON(字符串testName,长测试时间){
日期=新日期();
Timestamp ts=新的时间戳(date.getTime());
JSONObject obj=新的JSONObject();
obj.put(“testname”,testname);
obj.put(“持续时间”,测试时间);
对象put(“Timestamp”,ts.toString());
Gson Gson=new GsonBuilder().setPrettyPrinting().create();
JsonParser jp=新的JsonParser();
JsonElement je=jp.parse(obj.toJSONString());
jsoncontent=gson.toJson(je);
JsonArray JsonArray=新的JsonArray();
jsonArray.add(日本脑炎);
jsoncontent=gson.toJson(jsonArray);
}
公共静态无效writeJsonToFile(){
试一试{
文件f1=新文件(文件路径);
如果(!(f1.exists())){
f1.createNewFile();
}
FileWriter fw1=新的FileWriter(f1,true);
PrintWriter pw1=新的PrintWriter(fw1);
if(f1.exists()&&f1.isFile()){
pw1.println(jsoncontent);
pw1.flush();
pw1.close();
fw1.close();
}否则{
System.out.println(“请提供目标Json文件的有效路径”);
}
}捕获(例外e){
e、 printStackTrace();
}
}
}
您需要这样做

public class GetMetrics {

public static void clearFileContents(String filePath) throws IOException {
File f1 = new File(filePath);
new FileWriter(f1);
}

public static void metricAsJSON(String key, long value, String filePath) {

Date date = new Date();
Timestamp ts = new Timestamp(date.getTime());

JSONObject obj = new JSONObject();

obj.put(key, value);
obj.put("Timestamp", ts.toString());

try {
 Gson gson = new GsonBuilder().setPrettyPrinting().create();
 JsonParser jp = new JsonParser();
 JsonElement je = jp.parse(obj.toJSONString());
 String jsoncontent = = gson.toJson(je);

 File f1 = new File(filePath);

 if (!(f1.exists())) {
  f1.createNewFile();
 }

 FileWriter fw1 = new FileWriter(f1, true);

 PrintWriter pw1 = new PrintWriter(fw1);
 if (f1.exists() && f1.isFile()) {
  pw1.println(jsoncontent);
  pw1.flush();
  pw1.close();
  fw1.close();
 } else {
  System.out.println("Please provide a valid path to destination Jsonfile");
 }
} catch (Exception e) {
 e.printStackTrace();
 }
}
}

看起来您每个
JSONObject
都要使用一次函数,每次都要写入文件-如果是这种情况,那么您可以从该方法返回
JSONObject

/*... obj.put(key, value); obj.put('Timestamp') ... */
return obj;
然后在现在调用
.metricAsJSON
的地方,将其附加到
JSONArray
对象,如下所示:

JSONArray arr = new JSONArray();
for (... metric in some other array ...) {
  JSONObject obj = GetMetrics.metricAsJSON(key, value);
  arr.put(obj);
}
String jsonString = arr.toString();
/* Write jsonString to file */

如果需要动态添加值并多次写入,可以将
JSONArray
存储为方法变量,或者每次需要在末尾添加内容时将文件加载到
JSONArray
对象中。

使用Gson或Jackson Libraries谢谢-这会美化数据,但不会添加逗号来分隔对象,也不会添加方括号来打开和关闭建议的ArrayTank。我已经修改了密码。每次收集JsonObject字符串时,确实会调用GetMetricasJSon方法一次。我现在只需要将每个对象连接/附加到现有数组上。我现在正在写最后的文件。我想从这里开始应该是直截了当的!也许这是一个不同的问题,我现在记不起来了,但是您应该能够直接将.JSON文件加载到JSONArray中,这应该为您提供附加工具。祝你好运