后端json对象的使用和特性

后端也可以做json对象的存储,具体的使用实例如下:

1、数据模版

{
    "contractId": 2611141940251005036,
    "documentParams": [
        {
            "name": "乙方姓名2",
            "value": "邓茜茜"
        },
        {
            "name": "乙方姓名3",
            "value": "邓茜"
        }
    ]
}

2.1、具体使用——list

public Result<String> fillParams(@RequestBody JSONObject object){
        Result<String> result = new Result<>();
        String contractId = object.getString("contractId");
        String bizId = object.getString("bizId");
     //获取数据对象 JSONArray documentParams = object.getJSONArray("documentParams"); List<DocumentParamDto> documentParamDtos = documentParams.toJavaList(DocumentParamDto.class); try {
     //处理数据变量 result = contractLockService.fillParams(Long.valueOf(contractId),bizId,documentParamDtos); }catch (Exception e) { e.printStackTrace(); result.error500(e.getMessage()); } return result; }

2,.2、具体使用——map

  public Result<String> fillParams(@RequestBody Map<String,Object> map){
        Result<String> result = new Result<>();
        String contractId = map.get("contractId").toString();
        Object documentParams = map.get("documentParams");
        List<DocumentParamDto> documentParamDtos = JSON.parseArray(JSON.toJSONString(documentParams), DocumentParamDto.class);

        return result;
    }

2.3、hutool使用json

JSONObject object = JSONObject.parseObject(execute);
        String result1 = object.getString("result");

        if (ResponseCodeEnum.SUCCESS.getCode()
                .equals(Integer.parseInt(object.getString("code")))){
            result.setCode(200);
            JSONArray jsonArray = JSONUtil.parseArray(result1);
            result.setResult(JSONUtil.toList(jsonArray, TemplateEntity.class));
            result.setMessage(object.getString("message"));
            return result;
        }

 

posted @ 2023-02-24 14:37  不如俗且趣  阅读(55)  评论(0)    收藏  举报