[alibaba/fastjson]反序列化后字段值丢失

2024-09-02 119 views
6

测试用例

@Test
 public  void test02() throws Exception {
         JSONObject responseData = new JSONObject();
         Integer status=1;
         List<Map<String,Object>> list=new ArrayList<>();
         Map<String, Object> map = new HashMap<>();
         map.put("file_type","auth_protocol");
         map.put("file_path","http");
         list.add(map);
         responseData.put("data",list);
         responseData.put("status",status);
         String s = JSONObject.toJSONString(responseData);
         TestResp testResp = JSON.parseObject(s, TestResp.class);
 }
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestResp  implements Serializable {

    public final static long serialVersionUID=1L;

    @JsonProperty(value = "status")
    private Integer status;

    @JsonProperty(value = "data")
    private List<Data> data;

   /**
    get、set方法省略
  **/

}
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Data implements Serializable {

    public final static long serialVersionUID=1L;

    @JsonProperty(value = "file_type")
    private String fileType;
    @JsonProperty(value = "file_path")
    private String filePath;

    /**
     get、set方法省略
    **/

}

image

回答

9

试过了, 并未能复现

4

我这边每次都出现这个错,已经好久了

2

@TBOOMp 我也碰到了一样的情况,你直接拿responseData用JSONObject.toJavaObject方法转TestResp.class试试,当有字段的层级很深时,jsonString转javaObject(有问题),jsonObject转javaObject(没问题),我目前测出来的结果是这样的,希望对你有帮助