Java 如何使用json节点解析json数组值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23499721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:24:59  来源:igfitidea点击:

how to parse json array value using json node

javajsonHymanson

提问by pushpendra chauhan

I have a JsonNodewhich contains the following JSON. Inside that JsonNodeobject is an array. In that array there are three fields, one of which, slaid, is a list. The other two are strings. Here is the JSON.

我有一个JsonNode包含以下 JSON 的文件。该JsonNode对象内部是一个数组。在该数组中有三个字段,其中之一slaid是一个列表。另外两个是字符串。这是 JSON。

{
    "SLA": [
        {
            "slaid": [
                "53637cc144ae8b607e089701"
            ],
            "ragindicator": "Red",
            "name": "r1"
        },
        {
            "slaid": [
                "53637d1844ae8b607e089704"
            ],
            "ragindicator": "Amber",
            "name": "a1"
        },
        {
            "slaid": [
                "53637eac44ae8b607e089706"
            ],
            "ragindicator": "Green",
            "name": "g1"
        }
    ]
}

I want to parse this value. How can I parse it , where slaid's type is List<String>? I have tried some ways but I am still unable to find the solution.

我想解析这个值。我如何解析它,slaid类型在List<String>哪里?我尝试了一些方法,但仍然无法找到解决方案。

回答by Micha? Ziober

The easiest way I can see is creating POJOclasses which fit to your JSON:

我能看到的最简单的方法是创建POJO适合您的类JSON

class Slaids {

    @JsonProperty("SLA")
    private List<Slaid> slaids;

    public List<Slaid> getSlaids() {
        return slaids;
    }

    public void setSlaids(List<Slaid> slaids) {
        this.slaids = slaids;
    }

    @Override
    public String toString() {
        return slaids.toString();
    }
}

class Slaid {

    private List<String> slaid;
    private String ragindicator;
    private String name;

    public List<String> getSlaid() {
        return slaid;
    }

    public void setSlaid(List<String> slaid) {
        this.slaid = slaid;
    }

    public String getRagindicator() {
        return ragindicator;
    }

    public void setRagindicator(String ragindicator) {
        this.ragindicator = ragindicator;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Slaid [slaid=" + slaid + ", ragindicator=" + ragindicator + ", name=" + name + "]";
    }
}

Simple usage:

简单用法:

ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.readValue(json, Slaids.class));

Above program prints:

以上程序打印:

[Slaid [slaid=[53637cc144ae8b607e089701], ragindicator=Red, name=r1], Slaid [slaid=[53637d1844ae8b607e089704], ragindicator=Amber, name=a1], Slaid [slaid=[53637eac44ae8b607e089706], ragindicator=Green, name=g1]]

If you want to use JsonNodeyou can do it in this way:

如果你想使用JsonNode你可以这样做:

ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(json);
ArrayNode slaidsNode = (ArrayNode) rootNode.get("SLA");
Iterator<JsonNode> slaidsIterator = slaidsNode.elements();
while (slaidsIterator.hasNext()) {
    JsonNode slaidNode = slaidsIterator.next();
    System.out.println(slaidNode.get("slaid"));
    System.out.println(slaidNode.get("ragindicator"));
    System.out.println(slaidNode.get("name"));
}

Above program prints:

以上程序打印:

["53637cc144ae8b607e089701"]
"Red"
"r1"
["53637d1844ae8b607e089704"]
"Amber"
"a1"
["53637eac44ae8b607e089706"]
"Green"
"g1"