java - Get specific value from JSON array using java
问题描述
I want to fetch only PersonNumber value from below JSON sample using java.
{"Context":[{"PersonNumber":"10","DMLOperation":"UPDATE","PeriodType":"E","PersonName":"Ponce","WorkerType":"EMP","PersonId":"1000","PrimaryPhoneNumber":"1","EffectiveStartDate":"2018-01-27","EffectiveDate":"2018-01-27"}],"Changed Attributes":[{"NamInformation1":{"new":"Ponce","old":"PONCE"}},{"FirstName":{"new":"Jorge","old":"JORGE"}},{"LastName":{"new":"Ponce","old":"PONCE"}}]}
Below is my code:
for (SyndContentImpl content : (List<SyndContentImpl>) entry.getContents()) {
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(content.getValue().toString());
System.out.println(jsonObj.get("Context"));
} catch (JSONException e) {
e.printStackTrace();
} }
解决方案
您必须访问路径Context[0].PersonNumber
。
这可以通过
String personNumber = jsonObj.getJSONArray("Context").getJSONObject(0).getString("PersonNumber");
推荐阅读
- apache-spark - 带参数的默认 Spark 侦听器
- javascript - instanceof 在打字稿/角度项目之间没有按预期工作
- python - 我的 Tetration(复数)函数必须更好地矢量化(Python)
- mysql - 如何从 MySQL 中的电话号码中删除空格
- java - 我正在寻找一个 java 正则表达式,如果字符串包含任何字符超过 3 次,它将返回 true
- powershell - 检查每天是否存在超过 1 个刷新计划重复
- asp.net-core - 根据 ASP.NET Core 3 Razor Pages 中的选择列表项动态更改输入值
- java - 是否可以检查输入是否为没有 hasNextLine 的整数,如果为 0 则退出循环?
- java - 为什么 PathVariable 的要求不起作用
- css - 根据动态生成的下拉值在 Angular 9 中使用 *ngIf 动态显示或隐藏 Div