簡體   English   中英

如何將對象的 JSONArray 轉換為 json 字符串消息

[英]How to convert JSONArray of objects to a json string message

how can i create a JSON string from Json array of objects like below in Java using JSON object

{

    header: [
              {

               "key" : "numberOfRecords",
               "value" : "122"
               "valueDataType" : "string"
            
              },

              { 
                "key" : "g_udit"
                "value" : "1"
                "valueDataType" : "string"
              },
              {
                "key": "userNameId"
                "value" : "155"
                "valueDataType : "string"
              }
           ]
}

預期 JSON output 只需要值

{
  header :
         { 
            "numberOfRecords" : "122",
            "g_udit" : "1",
            "userNameId" : "155"
         }
}

使用 JSON 查詢語言轉換 JSON 結構。 一條Josson查詢語句就可以完成這項工作。

https://github.com/octomix/josson

Josson josson = Josson.fromJsonString(
    "{" +
    "    \"header\": [" +
    "              {" +
    "               \"key\" : \"numberOfRecords\"," +
    "               \"value\" : \"122\"," +
    "               \"valueDataType\" : \"string\"" +
    "              }," +
    "              { " +
    "                \"key\" : \"g_udit\"," +
    "                \"value\" : \"1\"," +
    "                \"valueDataType\" : \"string\"" +
    "              }," +
    "              {" +
    "                \"key\": \"userNameId\"," +
    "                \"value\" : \"155\"," +
    "                \"valueDataType\" : \"string\"" +
    "              }" +
    "           ]" +
    "}");
JsonNode node = josson.getNode("map(header.map(key::value).mergeObjects())");

Output

{
  "header" : {
    "numberOfRecords" : "122",
    "g_udit" : "1",
    "userNameId" : "155"
  }
}

首先你應該使用任何json框架來讀寫文件。 您可以使用jacskon-utils來使用Jackson並使其更易於使用。

然后您必須從輸入和 output 類型定義數據類。 最后,轉換數據。

@Getter
class InputData {

    @JsonProperty("header")
    private List<Header> headers;

    @Getter
    public static class Header {

        private String key;
        private String value;
        private String valueDataType;

    }

}

@Setter
class OutputData {

    @JsonProperty("header")
    private Map<String, String> headers;

}

public static void main(String... args) throws Exception {
    InputData inputData = readData(new File("c:/in.json"));
    OutputData outputData = createOutputData(inputData);
    writeData(new File("c:/out.json"), outputData);
}

private static InputData readData(File file) throws Exception {
    try (InputStream in = new FileInputStream(file)) {
        return JacksonUtils.readValue(in, InputData.class);
    }
}

private static void writeData(File file, OutputData outputData) throws Exception {
    try (OutputStream out = new FileOutputStream(file)) {
        JacksonUtils.prettyPrint().writeValue(outputData, out);
    }
}

private static OutputData createOutputData(InputData inputData) {
    Map<String, String> headers = new LinkedHashMap<>();
    inputData.getHeaders().forEach(header -> headers.put(header.getKey(), header.getValue()));

    OutputData outputData = new OutputData();
    outputData.setHeaders(headers);

    return outputData;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM