0

this is my code, and i can get strings like status, but for example "proxy" i cannot because is inside of "8.8.8.8", my question is, how can I get the value of "proxy" with GSON.

{
    "status": "ok",
    "8.8.8.8": {
        "asn": "AS15169",
        "provider": "Google LLC",
        "continent": "North America",
        "country": "United States",
        "isocode": "US",
        "latitude": 37.751,
        "longitude": -97.822,
        "proxy": "no",
        "type": "Business"
    }
}
    public static void main(String[] args) {

        URL url = null;

        try {
            url = new URL("https://proxycheck.io/v2/8.8.8.8?vpn=1&asn=1&tag=proxycheck.io");
            URLConnection request = url.openConnection();
            request.connect();

            JsonParser jp = new JsonParser();

            JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
            System.out.println(root);

            JsonObject rootobj = root.getAsJsonObject();

            JsonElement proxy = rootobj.get("8.8.8.8");
            System.out.println(proxy);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
ccr2
  • 1
  • 2
  • 1
    I believe you would need to create a custom object with the same fields (instance field names have to be the same as the keys I believe), and you can pass the class of the object to GSON, which parses and returns the object provided – Sal Dec 25 '20 at 00:03
  • 2
    If all you want to do is select one value from the `8.8.8.8` object, then you can drill down into that object. Starting with your existing `rootobj`, you can use this: `String proxy = rootobj.get("8.8.8.8").getAsJsonObject().get("proxy").getAsString();`. Having said that, I agree with @Sal that it's generally better to create POJOs which reflect the shape of your JSON. That way you can access any of your POJO attributes more easily. – andrewJames Dec 25 '20 at 01:23
  • 1
    Does this answer your question? [Using gson to deserialize specific JSON field of an object](https://stackoverflow.com/questions/11428329/using-gson-to-deserialize-specific-json-field-of-an-object) – andrewJames Dec 25 '20 at 01:23

2 Answers2

0

You can get the "proxy" field in "8.8.8.8" from that JSON object like this:

JsonObject rootobj = root.getAsJsonObject();
JsonElement network = rootobj.get("8.8.8.8");
String proxy = network.getAsJsonObject().get("proxy").getAsString();
System.out.println(proxy);

returns:

no

complete example:

import java.net.URL;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class Main {
    public static void main(String[] args) {
            String jsonString = "{"
                    + "    \"status\": \"ok\","
                    + "    \"8.8.8.8\": {"
                    + "        \"asn\": \"AS15169\","
                    + "        \"provider\": \"Google LLC\","
                    + "        \"continent\": \"North America\","
                    + "        \"country\": \"United States\","
                    + "        \"isocode\": \"US\","
                    + "        \"latitude\": 37.751,"
                    + "        \"longitude\": -97.822,"
                    + "        \"proxy\": \"no\","
                    + "        \"type\": \"Business\""
                    + "    }"
                    + "}";
            JsonParser jp = new JsonParser();

            JsonElement root = jp.parseString(jsonString);
            System.out.println(root);

            JsonObject rootobj = root.getAsJsonObject();

            JsonElement network = rootobj.get("8.8.8.8");
            String proxy = network.getAsJsonObject().get("proxy").getAsString();
            System.out.println(proxy);
    }
}
geocodezip
  • 158,664
  • 13
  • 220
  • 245
0
        String str = "{\"status\":\"ok\",\"8.8.8.8\":{\"asn\":\"AS15169\",\"provider\":\"Google LLC\",\"continent\":\"North America\",\"country\":\"United States\",\"isocode\":\"US\",\"latitude\":37.751,\"longitude\":-97.822,\"proxy\":\"no\",\"type\":\"Business\"}}";
        JsonObject jsonObject = new JsonParser().parse(str).getAsJsonObject();
        JsonObject data = jsonObject.getAsJsonObject("8.8.8.8");
        String proxy = data.get("proxy").getAsString();
        System.out.println(proxy);
HaiZi
  • 179
  • 1
  • 7