【问题标题】:From a nested JSON String getting a specific key's value从嵌套的 JSON 字符串中获取特定键的值
【发布时间】:2022-07-07 21:15:25
【问题描述】:

这里我有一个后映射类型的控制器。我将请求正文作为嵌套 JSON 的字符串。当使用字符串作为请求主体调用控制器时,我想将该字符串映射到 POJO。在那个 POJO 中,我有要从该嵌套 json 映射的字段,还有一个字段,它按原样采用实际的 String 请求正文。请帮助我如何将特定字段从嵌套的 json 字符串映射到 POJO。

请求看起来像 -

{
    "Application": {
        "DCode": "unsecliverelease",
        "PType": "DA",
        "AId": "230391106",
        "ApNO": "NTFLbjOF9fXI15AF1YiC",
        "crd": {
            "cate": "lion",
            "ProductCode": "lion"
        },
        "ld": {
            "dm": {
                "sn": "3",
                "RandomNumbers": {
                    "RandomNumber01": "319",
                    "RandomNumber02": "731",
                    "RandomNumber03": "520",
                    "RandomNumber04": "102",
                    "RandomNumber05": "678"
                },
                "Request": {
                    "Name": "MSE",
                    "ACount": "1",
                    "BrandInd": "wert",
                    "CID": "123456789",
                    
                }
            }
    }

//控制器

@PostMapping(
      value = "/decision",
      produces = MediaType.APPLICATION_JSON_VALUE,
      consumes = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<crdResponse > getDecision(
      @RequestBody final @Valid String request) throws JsonProcessingException {
    crdResponse response =
        crdService.getDec(request);

    return ResponseEntity.ok().body(response);
  }

//POJO

public class CRequestModel {

  @Column(name = "rid")
  @Id
  private String crdRqsId;

  @Column(name = "scode")
  private String scode;

  @Column(name = "cid")
  private Integer cid;

  @Column(name = "RequestNumber")
  private Integer requestNumber;

  @Column(name = "RequestJson")
  private String requestJSON;

  @Column(name = "CreatedAt")
  private Timestamp createdAt;
}

我想将整个 JSON 字符串保存到 requestJSON 字段中,并希望将 CID 值(来自请求 JSON STRING)保存到 cid 字段中。

请帮助我。此输入 JSON 字符串可以更改,因此 CID 在当前 JSON 字符串中的顺序可能会有所不同。

【问题讨论】:

    标签: java json spring spring-boot


    【解决方案1】:

    您发布的 JSON 无效。我想它应该是这样的:

    {
      "Application": {
        "DCode": "unsecliverelease",
        "PType": "DA",
        "AId": "230391106",
        "ApNO": "NTFLbjOF9fXI15AF1YiC",
        "crd": {
          "cate": "lion",
          "ProductCode": "lion"
        },
        "ld": {
          "dm": {
            "sn": "3",
            "RandomNumbers": {
              "RandomNumber01": "319",
              "RandomNumber02": "731",
              "RandomNumber03": "520",
              "RandomNumber04": "102",
              "RandomNumber05": "678"
            },
            "Request": {
              "Name": "MSE",
              "ACount": "1",
              "BrandInd": "wert",
              "CID": "123456789"
            }
          }
        }
      }
    }
    

    你可以做的是:

    String json ="{\n" +
                         "  \"Application\": {\n" +
                         "    \"DCode\": \"unsecliverelease\",\n" +
                         "    \"PType\": \"DA\",\n" +
                         "    \"AId\": \"230391106\",\n" +
                         "    \"ApNO\": \"NTFLbjOF9fXI15AF1YiC\",\n" +
                         "    \"crd\": {\n" +
                         "      \"cate\": \"lion\",\n" +
                         "      \"ProductCode\": \"lion\"\n" +
                         "    },\n" +
                         "    \"ld\": {\n" +
                         "      \"dm\": {\n" +
                         "        \"sn\": \"3\",\n" +
                         "        \"RandomNumbers\": {\n" +
                         "          \"RandomNumber01\": \"319\",\n" +
                         "          \"RandomNumber02\": \"731\",\n" +
                         "          \"RandomNumber03\": \"520\",\n" +
                         "          \"RandomNumber04\": \"102\",\n" +
                         "          \"RandomNumber05\": \"678\"\n" +
                         "        },\n" +
                         "        \"Request\": {\n" +
                         "          \"Name\": \"MSE\",\n" +
                         "          \"ACount\": \"1\",\n" +
                         "          \"BrandInd\": \"wert\",\n" +
                         "          \"CID\": \"123456789\"\n" +
                         "        }\n" +
                         "      }\n" +
                         "    }\n" +
                         "  }\n" +
                         "}";
        
        
    
    CRequestModel cRequestModel = new CRequestModel();
        cRequestModel.setRequestJSON(json);
    
        ObjectMapper mapper = new ObjectMapper();
        JsonNode actualObj = mapper.readTree(json);
        JsonNode cidNode = actualObj.get("Application").get("ld").get("dm").get("Request").get("CID");
        int cid= mapper.treeToValue(cidNode,Integer.class);
        cRequestModel.setCid(cid);
    

    注意:假设您获得的 json 将始终具有上述结构和属性:ld、dm、Request、CID。 (顺序无关紧要)

    【讨论】:

      猜你喜欢
      • 2020-12-02
      • 2023-03-22
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多