如何将一个JSON模式映射到另一个?

时间:2018-04-23 15:26:16

标签: java json

我有一个要求,我需要将输入JSON文件映射到特定的输出JSON模式,但不知道如何继续这样做。我应该继续从JSON转换为Java对象,转换并转换回JSON还是有其他方法来处理它?<​​/ p>

我的输入和输出JSON如下面的示例所示。

输入:

[{
  "FirstName": "fgfgf",
  "BIRTH_DATE": "1988-06-25",
  "frm": "sdsd67ds-4937-fdf",
  "GENDER_CODE": "M",
  "ID": "4083-9b78-eab99231adc9",
  "LastName": "ddgd"
}, {
  "FirstName": "gfgf",
  "BIRTH_DATE": "1973-03-02",
  "frm": "7e3b3dd6-97bc-d918065a642f",
  "GENDER_CODE": "O",
  "ID": "88e9-0253a00cde91",
  "LastName": "gfgsfg"
}, {
  "FirstName": " dfsdfsdfgsd",
  "BIRTH_DATE": "1950-02-26",
  "frm": "9d30-9f23ed77ea1a",
  "GENDER_CODE": "M",
  "ID": "9b1b-090c179a31ea",
  "LastName": "RU dddas"
}, {
  "cStage df": "cStage df",
  "dds": "cM1y",
  "ddsc": "cTgb",
  "grgfd": "cK1",
  "BId": "a431a5b4-4176-704bed5021f8",
  "fsfsdfsd": "7face4c8-ffa870cfbf2d"
}, {
  "fvf": "cStage dgd",
  "dsdf": "cMff",
  "fgsd": "cTgf",
  "FirstName": "dfdfsdsf",
  "dfs": "c1",
  "BId": "a43-9d6d-704bed5021f8",
  "BIRTH_DATE": "1999-08-30",
  "msdrsdn": "727146-8e930fc1f662",
  "GENDER_CODE": "F",
  "ID": "6560a6cb-e01757819bcc",
  "LastName": "dfsdfsd"
}]

输出:

[{
    "SENDER_NAME": "XYZ",
    "MARITAL_STATUS": "Unmarried",
    "GENDER_CODE": "F",
    "PHONE_NO": "576756754",
    "LAST_NAME": "ghghg",
    "FIRST_NAME": "hgdghd",
    "PHONE_CODE": "91",
    "GENDER": "F",
    "ID": "5765474",
    "ADDRESS": "500 fgdfgd,gdf",
    "BIRTH_DATE": 1523877361463,
    "gdfgg": "-86afad953b34",
    "DATE_TIME": 56546456,
    "A_BIRTH_DATE": "567546456",
    "C_INFO": {
      "CLINICAL": {
        "T": "1",
        "N": "0",
        "M": "1",
        "STAGE": "Stage I"
      },
      "PATHOLOGY": {
        "T": "1",
        "N": "1",
        "M": "1",
        "STAGE": "Stage I"
      }
    }
  },
  {
    "SENDER_NAME": "ABC",
    "MARITAL_STATUS": "Unmarried",
    "GENDER_CODE": "M",
    "PHONE_NO": "56456456",
    "LAST_NAME": "hghfg",
    "FIRST_NAME": "dgdfgd",
    "PHONE_CODE": "91",
    "GENDER_CODE": "M",
    "ID": "6456456456",
    "FULL_ADDRESS": "fgdfdfghdf,fdgfdg,fgfd45345",
    "BIRTH_DATE": 1523877413779,
    "ID": "b84e-4866a656cce8",
    "DATE_TIME": 1523877413779,
    "BIRTH_DATE": "67567657",
    "C_INFO": {
      "CLINICAL": {
        "T": "0",
        "N": "0",
        "M": "0",
        "STAGE": "Stage 0"
      },
      "PATHOLOGY": {
        "T": "0",
        "N": "0",
        "M": "0",
        "STAGE": "Stage 0"
      }
    }
  },
  {
    "SENDER_NAME": "RAC",
    "MARITAL_STATUS": "Unmarried",
    "GENDER_CODE": "F",
    "PHONE_NO": "56546546",
    "LAST_NAME": "hfghf",
    "FIRST_NAME": "gfhfghfg",
    "PHONE_CODE": "91",
    "GENDER_CODE": "M",
    "ID": "676745674",
    "_ADDRESS": "fgdfdfg",
    "BIRTH_DATE": 6756756756,
    "ID": "2e-a0d8-hghfg67ggh",
    "DATE_TIME": "Mon March 20 17:52:04 IST 2018",
    "BIRTH_DATE": "05212014",
    "C_INFO": {
      "CLINICAL": {
        "T": "0",
        "N": "0",
        "M": "1",
        "STAGE": "Stage IA"
      },
      "PATHOLOGY": {
        "T": "1",
        "N": "0",
        "M": "1",
        "STAGE": "Stage IB"
      }
    }
  }
]

2 个答案:

答案 0 :(得分:1)

Jolt。这将允许您进行无POJO转换,只要格式不是非常不同。

答案 1 :(得分:0)

您可以使用Jackson API添加字段或转换JSON而无需创建POJO。它提供了一个名为JsonNodeJsonObjectJsonArray类型的对象形式,可以像我在下面的代码中那样进行转换。我希望这会对你有所帮助。

import java.io.IOException;
import java.util.Iterator;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class JsonTest {

    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws JsonProcessingException, IOException {
        String jsoninpt = "[{\r\n" + 
                "  \"FirstName\": \"fgfgf\",\r\n" + 
                "  \"BIRTH_DATE\": \"1988-06-25\",\r\n" + 
                "  \"frm\": \"sdsd67ds-4937-fdf\",\r\n" + 
                "  \"GENDER_CODE\": \"M\",\r\n" + 
                "  \"ID\": \"4083-9b78-eab99231adc9\",\r\n" + 
                "  \"LastName\": \"ddgd\"\r\n" + 
                "}, {\r\n" + 
                "  \"FirstName\": \"gfgf\",\r\n" + 
                "  \"BIRTH_DATE\": \"1973-03-02\",\r\n" + 
                "  \"frm\": \"7e3b3dd6-97bc-d918065a642f\",\r\n" + 
                "  \"GENDER_CODE\": \"O\",\r\n" + 
                "  \"ID\": \"88e9-0253a00cde91\",\r\n" + 
                "  \"LastName\": \"gfgsfg\"\r\n" + 
                "}, {\r\n" + 
                "  \"FirstName\": \" dfsdfsdfgsd\",\r\n" + 
                "  \"BIRTH_DATE\": \"1950-02-26\",\r\n" + 
                "  \"frm\": \"9d30-9f23ed77ea1a\",\r\n" + 
                "  \"GENDER_CODE\": \"M\",\r\n" + 
                "  \"ID\": \"9b1b-090c179a31ea\",\r\n" + 
                "  \"LastName\": \"RU dddas\"\r\n" + 
                "}, {\r\n" + 
                "  \"cStage df\": \"cStage df\",\r\n" + 
                "  \"dds\": \"cM1y\",\r\n" + 
                "  \"ddsc\": \"cTgb\",\r\n" + 
                "  \"grgfd\": \"cK1\",\r\n" + 
                "  \"BId\": \"a431a5b4-4176-704bed5021f8\",\r\n" + 
                "  \"fsfsdfsd\": \"7face4c8-ffa870cfbf2d\"\r\n" + 
                "}, {\r\n" + 
                "  \"fvf\": \"cStage dgd\",\r\n" + 
                "  \"dsdf\": \"cMff\",\r\n" + 
                "  \"fgsd\": \"cTgf\",\r\n" + 
                "  \"FirstName\": \"dfdfsdsf\",\r\n" + 
                "  \"dfs\": \"c1\",\r\n" + 
                "  \"BId\": \"a43-9d6d-704bed5021f8\",\r\n" + 
                "  \"BIRTH_DATE\": \"1999-08-30\",\r\n" + 
                "  \"msdrsdn\": \"727146-8e930fc1f662\",\r\n" + 
                "  \"GENDER_CODE\": \"F\",\r\n" + 
                "  \"ID\": \"6560a6cb-e01757819bcc\",\r\n" + 
                "  \"LastName\": \"dfsdfsd\"\r\n" + 
                "}]";

        String field = "{\r\n" + 
                "      \"CLINICAL\": {\r\n" + 
                "        \"T\": \"1\",\r\n" + 
                "        \"N\": \"0\",\r\n" + 
                "        \"M\": \"1\",\r\n" + 
                "        \"STAGE\": \"Stage I\"\r\n" + 
                "      },\r\n" + 
                "      \"PATHOLOGY\": {\r\n" + 
                "        \"T\": \"1\",\r\n" + 
                "        \"N\": \"1\",\r\n" + 
                "        \"M\": \"1\",\r\n" + 
                "        \"STAGE\": \"Stage I\"\r\n" + 
                "      }\r\n" + 
                "    }";


        //create JSON Node object from JSON String using ObjectMapper
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonFieldNode = mapper.readTree(field);
        JsonNode jsonInputNode = mapper.readTree(jsoninpt);

        System.out.println(mapper.writeValueAsString(jsonInputNode));
        //Iterate through the JsonNode elements inside the JSON Array
        Iterator<JsonNode> jsonInputNodeElements = jsonInputNode.elements();

        while(jsonInputNodeElements.hasNext()) {
            ObjectNode element = (ObjectNode) jsonInputNodeElements.next();
            //Add element to the JSON Object.
            element.put("c_info", mapper.writeValueAsString(jsonFieldNode));
        }

        System.out.println(mapper.writeValueAsString(jsonInputNode));

    }

}