如何使用JsonObjectBuilder获取换行格式的json文件?

时间:2018-10-23 13:01:05

标签: java json jsonbuilder

我的Java程序创建的json文件如下所示:

{"ATA_UTP_RequiredFields":{"userId":"NA","utpPlatform":"NA"},"Template":{"CR Type":"NA","# of test runs":"NA"},"TestConfiguration":{"CID":"NA","Campaign submitter id":"NA"},"TestSetupDetails":{"LogName":"NA","LogDetails":"NA","FailureType":"NA"}}

所有内容都在一行中。虽然我希望它看起来像下面这样:

{
"ATA_UTP_RequiredFields":{
    "userId":"NA",
    "utpPlatform":"NA"
},
"Template":{
    "CR Type":"NA",
    "# of test runs":"NA"
},
"TestConfiguration":{
    "CID":"NA",
    "Campaign submitter id":"NA"
},
"TestSetupDetails":{
    "LogName":"NA",
    "LogDetails":"NA",
    "FailureType":"NA"
}
}

正确地由换行和制表符格式化。 我进行了很多搜索,但找不到解决方法。

这是我的示例代码:

     utpJsonFormat utpJsonFormatObj = new utpJsonFormat();
   JsonObjectBuilder utpJsonBuilder = Json.createObjectBuilder();
   JsonObjectBuilder ATA_UTP_RequiredFields = Json.createObjectBuilder();
   JsonObjectBuilder Template = Json.createObjectBuilder();
   JsonObjectBuilder TestConfiguration = Json.createObjectBuilder();
   JsonObjectBuilder TestSetupDetails = Json.createObjectBuilder();
   JsonObjectBuilder ToolsOutput = Json.createObjectBuilder();

   ATA_UTP_RequiredFields.add("userId",utpJsonFormatObj.getUserId());
   ATA_UTP_RequiredFields.add("utpPlatform",utpJsonFormatObj.getUTPPlatform());
   Template.add("CR Type",utpJsonFormatObj.getCrash());
   Template.add("# of test runs",utpJsonFormatObj.getNoOfTestRuns());

   TestConfiguration.add("CID",utpJsonFormatObj.getCID());
   TestConfiguration.add("Campaign submitter id",utpJsonFormatObj.getCampaignSubmitterID());
   TestConfiguration.add("Campaign submitter id",utpJsonFormatObj.getCampaignSubmitterName());

   TestSetupDetails.add("LogName",utpJsonFormatObj.getlogName());
   TestSetupDetails.add("LogDetails",utpJsonFormatObj.getlogDetails());
   TestSetupDetails.add("FailureType",utpJsonFormatObj.getFailureType());
   utpJsonBuilder.add("ATA_UTP_RequiredFields",ATA_UTP_RequiredFields);
   utpJsonBuilder.add("Template",Template);
   utpJsonBuilder.add("TestConfiguration",TestConfiguration);
   utpJsonBuilder.add("TestSetupDetails",TestSetupDetails);
   JsonObject utpJsonObject = utpJsonBuilder.build();
   try {
        //write to file
        OutputStream os = new FileOutputStream("C:\\FT_Project\\javaPrograms\\utpTicket.json");
        JsonWriter jsonWriter = Json.createWriter(os);

        jsonWriter.writeObject(utpJsonObject);
        jsonWriter.close();
    } catch (Exception ex) {
        System.out.println("exception");
    }

请帮助我如何进行排序。

0 个答案:

没有答案