简体   繁体   中英

How to filter json result using java jersey

How to filter the Json result to get the particular fields. Can any one help me to overcome this confusion. Below is the java programming code using jersey client. This program use virus total API key to fetch the data from the virus total. I got the result in Json format and my aim to filter the Json result and get particular fields rather than all fields

public static void main(String[] args) 
    {
        String ip = "13.57.233.180"; 

        Client client = Client.create();

        WebResource resource = client.resource("https://www.virustotal.com/vtapi/v2/ip-address/report?ip="+ip+"&apikey="+API_KEY);

        ClientResponse response = resource.accept("appliction/json").get(ClientResponse.class);

        if(response.getStatus() != 200)
        {
            throw new RuntimeException("Failed Error Code : "+response.getStatus());
        }

        String output = response.getEntity(String.class);
        System.out.println("");
        System.out.println("Output from the server");
        System.out.println("");
        System.out.println(output);
    }

I got the output as

{
    "country": "US",
    "response_code": 1,
    "detected_urls": [],
    "resolutions": [
        {
            "last_resolved": "2018-01-28 00:00:00",
            "hostname": "ec2-13-57-233-180.us-west-1.compute.amazonaws.com"
        }
    ],
    "verbose_msg": "IP address in dataset"
}

And I need to do filter in that result. I mean I need only country and verbose message fields. please suggest me how to overcome with this problem?

First of all, please correct the resouse type in your code as there is a mistake in the spelling of application/json

You can get your data into a Map/Hashmap using Jackson for Java. Add this dependency in your code for Jackson Code:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.4</version>
</dependency>

Then add the following lines to your code:

String output = response.getEntity(String.class);
Map<String, Object> map = new ObjectMapper().readValue(output,HashMap.class);
System.out.println("Country: " + map.get("country"));
System.out.println("Verbose Message: " + map.get("verbose_msg"));

This will fetch your data from String json ( output ) and map it on your hashmap.

I hope this is the solution to your problem. Thanks.

If you don't want to build a specific DTO to map you result as a Java Object try the following which will map your result as a Java Map and you'll be able to select the desired fields by using their name as the key:

Map response = resource.accept("application/json").get(Map.class);

System.out.printf("Country: %s\n", map.get("country"));
System.out.printf("Verbose Message: %s\n", map.get("verbose_msg"));

This solution is similar to the one proposed by @Safeer, but mapping is done directly by Jersey, you don't need to convert to String and then to Map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM