如何用gson读取子类json

agyaoht7  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(338)

这是我的代码,我可以得到像status这样的字符串,但是例如“proxy”我不能,因为它在“8.8.8.8”中,我的问题是,如何用gson获得“proxy”的值。

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

    }
3j86kqsm

3j86kqsm1#

您可以从json对象获取“8.8.8.8”中的“proxy”字段,如下所示:

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

退货:

完整示例:

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

piok6c0g2#

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

相关问题