Java 从JSON对象数组中检索值
我试图从数组中的对象中检索2个值。 我想获取位置(任务=续订,任务状态为完成) 对象:Java 从JSON对象数组中检索值,java,arrays,json,Java,Arrays,Json,我试图从数组中的对象中检索2个值。 我想获取位置(任务=续订,任务状态为完成) 对象: "policies": [ { "policyNumber": "000000000000", "policyDetails": { "a": "55375", "b": "2018-01-20", "c": "060", "d": "071", "e": "IL", "f": "0
"policies": [
{
"policyNumber": "000000000000",
"policyDetails": {
"a": "55375",
"b": "2018-01-20",
"c": "060",
"d": "071",
"e": "IL",
"f": "012",
"g": "A",
"h": "G"
},
"tasks": [
{
"task": "bills",
"eligibility": false,
"intent": null,
"taskStatus": "Status Unavailable",
"enrollmentStatus": false,
"orderNumber": "",
"reasonDescription": null
},
{
"task": "renewals",
"eligibility": null,
"intent": null,
"taskStatus": "Not Applicable",
"enrollmentStatus": false,
"orderNumber": "1",
"reasonDescription": null
}
]
}
]
为了获得价值,我可以做一些类似的事情吗
if (cPolicy.get().getTasks().getTask("renewals")&& cPolicy.get().getTasks().getTaskStatus("complete") {
}
如何获取值?这里是您的解决方案,我只是简单地修改了您的JSON
JsonNode jsonNode = objectMapper.readTree(from);
jsonNode.get("policies").get(0).get("tasks").get(0).get("eligibility")
范例
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Json {
public static void main(String[] args) {
File from = new File("abc.txt");
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode jsonNode = objectMapper.readTree(from);
System.out.println(jsonNode.get("policies").get(0).get("tasks").get(0).get("eligibility"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
JSON示例文件abc.txt
{
"policies": [
{
"policyNumber": "000000000000",
"policyDetails": {
"a": "55375",
"b": "2018-01-20",
"c": "060",
"d": "071",
"e": "IL",
"f": "012",
"g": "A",
"h": "G"
},
"tasks": [
{
"task": "bills",
"eligibility": false,
"intent": null,
"taskStatus": "Status Unavailable",
"enrollmentStatus": false,
"orderNumber": "",
"reasonDescription": null
},
{
"task": "renewals",
"eligibility": null,
"intent": null,
"taskStatus": "Not Applicable",
"enrollmentStatus": false,
"orderNumber": "1",
"reasonDescription": null
}
]
}
]
}
语言是什么?嗨!当你问问题时,文本区右侧有一个大橙色的“如何设置格式”框,其中包含有用的信息。还有一个工具栏,上面满是格式辅助工具。还有一个[?]按钮提供格式化帮助。还有一个预览区,显示你的帖子发布时的样子,位于文本区和“发布你的问题”按钮之间(这样你就必须滚动过去才能找到按钮,以鼓励你查看)。让你的帖子清晰明了,并证明你花了时间这样做,提高了你获得好答案的机会。请阅读,环顾四周,通读,特别是请指定你在标记学中使用的编程语言,这是在Java中。谢谢,这是一个很大的帮助