简体   繁体   English

将带有密钥动态的 json 转换为 POJO .. java spring 引导

[英]Convert json with key dynamic to POJO.. java spring boot

I need to convert the json into a pojo I already have the service that consumes it but I want to take it to a POJO to be able to handle it我需要将 json 转换为 pojo 我已经拥有使用它的服务,但我想将它带到 POJO 以便能够处理它

this is all Json:这都是 Json:

{
"meta": {
"status": "success",
"version": "2.0"
},
"data": {
"id": "M!t!Ch!v1!0000100002-5gda5bq0059b1-1245432868",
"token": "d71b160d82dfeeca87aa580f526d8570",
"privileges": {
  "__GLOBAL__": {
    "Store": {
      "1-m!i!s-045-158": {
        "*": -1
      },
      "1-m!i!s-047-324": {
        "*": -1
      },
      "1-m!i!s-031-1491": {
        "*": -1
      },
      "1-m!i!s-046-2": {
        "*": -1
      }
    }
  }
 },
"data": {
   "accessorId": 100001
 }
 }
 }

this part all keys is dinamic:这部分所有键都是动态的:

"1-m!i!s-045-158":{"*":-1} // **the key: 1-m!i!s-045-158 is dinamic** 
{"1-m!i!s-045-158":{"*":-1},"1-m!i!s-047-324":{"*":-1},"1-m!i!s-031-1491":{"*":-1},"1-m!i!s-046-2":{"*":-1}}

this is my controller这是我的 controller

    public JsonNode Evento(JsonNode json) {


    HttpHeaders header = new HttpHeaders();
    //header.add("Content-Type", "application/json");
    RestTemplate template = new RestTemplate();
    JsonNode node = mapper.convertValue(json  , JsonNode.class);
    HttpEntity<?> request = new HttpEntity<>(node);
    JsonNode resultado = template.postForObject("http://localhost:8083/moddo-channels/api/miinto", request, JsonNode.class);


    System.out.println(json.findPath("stote"));
    System.out.println(resultado);

    return json;

}

You must write POJO with all fields,您必须使用所有字段编写 POJO,

private Meta meta;

and

public class Meta{
   private String status;
   private String version;
}

etc in this style, problem can was with Store - it's must be Map等这种风格,问题可能出在商店 - 它必须是 Map

After this, you can take Jackson ObjectMapper and read object from your response在此之后,您可以使用 Jackson ObjectMapper 并从您的响应中读取 object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM