Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 来自WikipediaAPI和GSON的JSON-使用不同名称解析对象_Java_Json_Gson_Wikipedia Api - Fatal编程技术网

Java 来自WikipediaAPI和GSON的JSON-使用不同名称解析对象

Java 来自WikipediaAPI和GSON的JSON-使用不同名称解析对象,java,json,gson,wikipedia-api,Java,Json,Gson,Wikipedia Api,我正试图找到一个解析WikipediaAPI中上述JSON结果的解决方案。 问题是,由于“29839198”对象(在本例中)在每个API调用中都会有所不同,因此我似乎无法找到解析它的解决方案 这就是我到目前为止所做的: { "batchcomplete": "", "continue": { "grncontinue": "0.262157292819|0.262157407383|17998004|0", "continue": "grncontinue||" }, "quer

我正试图找到一个解析WikipediaAPI中上述JSON结果的解决方案。 问题是,由于“29839198”对象(在本例中)在每个API调用中都会有所不同,因此我似乎无法找到解析它的解决方案

这就是我到目前为止所做的:

{
"batchcomplete": "",
"continue": {
    "grncontinue": "0.262157292819|0.262157407383|17998004|0",
    "continue": "grncontinue||"
},
"query": {
    "pages": {
        "29839198": {
            "pageid": 29839198,
            "ns": 0,
            "title": "Paradox (film)",
            "extract": "Paradox is a 2010 science-fiction television film starring Kevin Sorbo, Steph Song and Christopher Judge, directed by Brenton Spencer, and based on a three-part graphic novel mini-series by Christos Gage.\n\n"
        }
    }
}

通常情况下,我会关闭带有对象名的“?”部分。

使用entrySet获取JsonElement:

URL url = new URL("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&explaintext=&generator=random&grnnamespace=0");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();

JsonElement jsonElement = new JsonParser().parse(new InputStreamReader((InputStream) request.getContent()));
JsonElement page = jsonElement.getAsJsonObject().get("?????");
URL=新URL(“https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&explaintext=&generator=random&grnnamespace=0");
HttpURLConnection请求=(HttpURLConnection)url.openConnection();
request.connect();
JsonElement JsonElement=newJSONParser().parse(newInputStreamReader((InputStream)request.getContent());
JsonElement pages=JsonElement.getAsJsonObject().get(“查询”).getAsJsonObject().get(“页面”);
Set entrySet=pages.getAsJsonObject().entrySet();
JsonElement yourDesiredElement=null;
for(Map.Entry:entrySet){
yourDesiredElement=entry.getValue();
}
//您的DesiredElement的空检查

希望这有帮助。

页面不是地图吗?我建议使用。是的,您必须编写更多带有注释的类,但我认为它提供了一个很好的抽象,当您在几年内修改代码时,您仍然能够理解您的代码。@beresfordt是吗?看起来像是嵌套的JSON对象me@Petter看起来像是一张地图,原因如下:pages.29839198.pageid=29839198,pages是复数,表示它下面可以有多个对象,29839198不是一个有意义的对象名称,而是一个合理的名称key@beresfordt在这种情况下,如何从
JsonElement
中提取映射?谢谢!使用entrySet是解决方案:)
        URL url = new URL("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&explaintext=&generator=random&grnnamespace=0");
        HttpURLConnection request = (HttpURLConnection) url.openConnection();
        request.connect();

        JsonElement jsonElement = new JsonParser().parse(new InputStreamReader((InputStream) request.getContent()));
        JsonElement pages = jsonElement.getAsJsonObject().get("query").getAsJsonObject().get("pages");

        Set<Entry<String, JsonElement>> entrySet = pages.getAsJsonObject().entrySet();

        JsonElement yourDesiredElement = null;

        for(Map.Entry<String,JsonElement> entry : entrySet){
            yourDesiredElement = entry.getValue();
        }

        //null check for yourDesiredElement