Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

java - How to read subclass JSON with GSON

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();
        }

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...