原创

Java-序列化专题-TypeReference-json对象转成较为复杂实体-映射成Map(key,实体)

public static void main(String[] args) {
String result = "{\n" +
" \"success\": true,\n" +
" \"message\": null,\n" +
" \"result\": {\n" +
" \"333333333SCHO/2020-09/SCHO/d2d6e24f5bee79b1e63502cbd26e79f8.mp3\": {\n" +
" \"id\": 196,\n" +
" \"containerId\": \"\",\n" +
" \"domain\": \"UNKNOWN\",\n" +
" \"objectId\": \"333333333SCHO/2020-09/SCHO/d2d6e24f5bee79b1e63502cbd26e79f8.mp3\",\n" +
" \"convStatus\": 2,\n" +
" \"convErr\": 0,\n" +
" \"convResultDesc\": \"\",\n" +
" \"lastSuccessTime\": \"2021-12-21T15:47:38\",\n" +
" \"withoutCheckUploaded\": 1,\n" +
" \"convertedContent\": \"SCHO/2020-09/SCHO/d2d6e24f5bee79b1e63502cbd26e79f8.mp3\",\n" +
" \"assetsId\": \"\",\n" +
" \"lastStartTime\": \"2021-12-21T15:16:11\",\n" +
" \"createTime\": \"2021-12-21T15:47:33\",\n" +
" \"duration\": 141,\n" +
" \"pages\": 0,\n" +
" \"fileType\": 2,\n" +
" \"fileMd5\": \"d2d6e24f5bee79b1e63502cbd26e79f8\",\n" +
" \"upload\": null\n" +
" }\n" +
" }\n" +
"}";
String key = "333333333SCHO/2020-09/SCHO/d2d6e24f5bee79b1e63502cbd26e79f8.mp3";
JSONObject json = JSON.parseObject(result);
JSONObject convertResult = json.getJSONObject("result");
//注意新的技术点,JSON 转 Map<String, 实体> 用法(以下不同的类)
Map<String, FileConvert> map = JSON.parseObject(convertResult.toJSONString(), new com.alibaba.fastjson.TypeReference<Map<String, FileConvert>>() {
});
FileConvert fileConvert = map.get(key);
System.out.println(fileConvert);
}
正文到此结束
本文目录