[英]From a nested JSON String getting a specific key's value
在这里,我有一个后映射类型的控制器。 我将请求正文作为嵌套 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 字符串中的顺序可能会有所不同。
您发布的 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。 (顺序无关紧要)
如果您有可以更改的 JSON 字符串,我认为您必须创建一个将该 JSON 字符串解析为jsonobject
的方法,然后从指定的“键”中提取一个值。
例如,如果您有一个如此组合的 JSON 字符串,
{
"name":"Sam",
"surname":"Baudo"
}
String jsonStringInput = "input-json-value";
JSONObject json = new JSONObject(jsonStringInput);
String surname = json.getString("surname");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.