Java converts String type json to json object and extracts the corresponding value
Background: I asked for a temporary fake data for format conversion and access to other people's docking projects
1. Things are too big, simplify
json.cn
link: json.cn
is much clearer.
2. Save it in the java demo
Because many " " are used in this json, java will perform escaping, some can escape normally, and some will report an error like this
Expect ':' at 0, actual "The error of escaping failed (this is a copy problem, copy one less " or /)
can be directly compressed and escaped in the link: json.cn after reading the data and use the copy inside button
copied into java code
3. maven dependency
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
4. Code
package com.ceshi;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class aa {
public static void main(String[] args) {
String result = "{\"filecount\":\"60\",\"filenames\":\"flowable_holiday-master.zip,jdchain-master.zip,flowable_holiday-master2.zip,flowable_holiday-master3.zip\",\"filenamesfilepath\":\"{\\\"flowable_holiday-master.zip\\\":\\\"D:/TY_DMS/原始备份区/DC-20220718714000062/001-001-002/flowable_holiday-master.zip\\\",\\\"flowable_holiday-master2.zip\\\":\\\"D:/TY_DMS/原始备份区/DC-20220718714000064/001-001-002/flowable_holiday-master2.zip\\\",\\\"flowable_holiday-master3.zip\\\":\\\"D:/TY_DMS/原始备份区/DC-20220718714000064/001-001-002/flowable_holiday-master3.zip\\\",\\\"jdchain-master.zip\\\":\\\"D:/TY_DMS/原始备份区/DC-20220718714000062/001-001-002/jdchain-master.zip\\\"}\",\"filenamesmd5\":\"{\\\"flowable_holiday-master.zip\\\":\\\"5a1cc37785764aaa4fd0c9e11fe940fc\\\",\\\"flowable_holiday-master2.zip\\\":\\\"5a1cc37785764aaa4fd0c9e11fe940fc\\\",\\\"flowable_holiday-master3.zip\\\":\\\"5a1cc37785764aaa4fd0c9e11fe940fc\\\",\\\"jdchain-master.zip\\\":\\\"f4f6a6363af4a93e717dd6197065de5b\\\"}\",\"files\":\"D:/TY_DMS/原始备份区/DC-20220718714000062/001-001-002/flowable_holiday-master.zip,D:/TY_DMS/原始备份区/DC-20220718714000064/001-001-002/flowable_holiday-master2.zip,D:/TY_DMS/原始备份区/DC-20220718714000064/001-001-002/flowable_holiday-master3.zip,D:/TY_DMS/原始备份区/DC-20220718714000062/001-001-002/jdchain-master.zip\",\"id\":\"13129224qweqweqeqe123\",\"md5\":\"3f377b490662d5d3de2c8665011f39f5\",\"pch\":\"1\",\"size\":\"1GB\",\"sl\":\"30\",\"大类\":\"文书档案\",\"小类\":\"案卷目录\",\"年度\":\"2022\",\"来源\":\"馆藏系统\",\"档号\":\"001-001-002\",\"版本号\":\"20220718164807\",\"题名\":\"xxxxx的通知\"}";
//将String字符转为Json对象
JSONObject jsonObject = JSON.parseObject(result);
//获取当前嵌套下的属性
String status = jsonObject.getString("md5");
if (status!=null){
System.out.println(status);
}
// //获取嵌套中的json串,细心观察 content为json数组,里面可放多个json对象
// JSONArray jsonArray = jsonObject.getJSONArray("content");
//
// //将json数组中取出一个json ,当前只有一个json组,所以下标为0
// JSONObject jsonFirst = jsonArray.getJSONObject(0);
//
// //取出这个json中的值
// String yongjin_type = jsonFirst.getString("yongjin_type");
// if (yongjin_type!=null){
// System.out.println(yongjin_type);
// }
}
}