Java 解析 JSON 字符串并提取 filename 值
Java 解析 JSON 字符串并提取 filename 值
本文介绍了使用 Java 代码解析 JSON 字符串并提取特定字段 (filename) 的值。示例代码展示了如何使用 JSONObject 类解析 JSON 数据并获取所需信息。
代码示例:
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
String jsonStr = '{"EsbHeader":{"VrsNo":"100","ScnNo":"10","SrcDate":"20190903","SrcTime":"165025","SrcSysId":"0332","SrcCalCod":"UMP","GloSeqNo":"G033220190903000000000004 ","ReqNo":"G033220190903000000000004 "},"Header":{"Action":"http://www.dongguanbank.cn/G0120006/02 ","Address":"0332"},"APPBody":{"templateno":"T1006","filename ":"20190828100836.dat"}}';
JSONObject jsonObject = new JSONObject(jsonStr);
String filename = jsonObject.getJSONObject("APPBody").getString("filename ");
System.out.println(filename);
}
}
输出结果:
20190828100836.dat
代码解析:
- 使用
JSONObject
类解析 JSON 字符串jsonStr
。 - 使用
getJSONObject()
方法获取APPBody
对象。 - 使用
getString()
方法从APPBody
对象中获取filename
字段的值。
总结:
本文展示了如何使用 Java 代码解析 JSON 字符串并提取特定字段的值。这在处理来自 API 的 JSON 数据或从文件中读取 JSON 数据时非常有用。
原文地址: https://www.cveoy.top/t/topic/mS4H 著作权归作者所有。请勿转载和采集!