java—解析来自wikipedia的json响应时遇到问题

hivapdat  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(309)

因此,我正在制作一个android应用程序,搜索mediawiki api,以获取有关某些名人的简短信息。
我们的想法是,您可以输入任何名称,它将提供mediawikiapi关于该名称/人员的信息,但目前我只使用一个名称,直到我找到正确解析json的方法。
我需要这个json响应的extract字段:json响应
这就是我目前的情况,我认为问题出在query类中,我只是不知道该类需要什么来确保只返回extract字段。当我运行这个程序时,onresponse()方法的输出就是null。谢谢你的帮助。
好的,我做了建议的更改,这是我的更新代码:

;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Map;

public class game extends AppCompatActivity {

private static final String ENDPOINT = "https://en.wikipedia.org/w/api.php?    format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Harry%20Potter";

private RequestQueue requestQueue;

private Gson gson;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    requestQueue = Volley.newRequestQueue(this);

    GsonBuilder gsonBuilder = new GsonBuilder();
    gson = gsonBuilder.create();
}

public void fetchPerson(View view)
{
    fetchPosts();
}
private void fetchPosts() {
    StringRequest request = new StringRequest(Request.Method.GET, ENDPOINT, onPostsLoaded, onPostsError);

    requestQueue.add(request);
}

private final Response.Listener<String> onPostsLoaded = new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        final TextView text = (TextView) findViewById(R.id.textView);

        Page page = gson.fromJson(response, Page.class);

        text.setText(page.extract);

    }
};

private final Response.ErrorListener onPostsError = new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e("PostActivity", error.toString());
    }
};
public class Root {
    String batchcomplete;
    Query  query;
}
public class Query {
    Map<String, Page> pages;
}
public class Page {
    int    pageid;
    int    ns;
    String title;
    String extract;
}

}

k5hmc34c

k5hmc34c1#

我认为问题出在我们的范围之内 Query
你说得对。
json数据如下所示:

{
  "batchcomplete": "",
  "query": {
    "pages": {
      "23140032": {
        "pageid": 23140032,
        "ns": 0,
        "title": "Frodo Baggins",
        "extract": "Frodo Baggins is a fictional character in J. R. R. Tolkien's legendarium, and the main protagonist of The Lord of the Rings. Frodo is a hobbit of the Shire who inherits the One Ring from his cousin Bilbo Baggins and undertakes the quest to destroy it in the fires of Mount Doom. He is also mentioned in Tolkien's posthumously published works, The Silmarillion and Unfinished Tales."
      }
    }
  }
}

根对象有2个字段: batchcomplete 以及 query .
尝试使用以下字段解析对象: extract 以及 title .
你看到不一致了吗?
如果您想使用gson,您需要为所有对象提供类。

class Root {
    String batchcomplete;
    Query  query;
}
class Query {
    Map<String, Page> pages;
}
class Page {
    int    pageid;
    int    ns;
    String title;
    String extract;
}

更新
您需要将json解析为 Root 对象。
示例代码:

String json = "{\n" +
              "  \"batchcomplete\": \"\",\n" +
              "  \"query\": {\n" +
              "    \"pages\": {\n" +
              "      \"23140032\": {\n" +
              "        \"pageid\": 23140032,\n" +
              "        \"ns\": 0,\n" +
              "        \"title\": \"Frodo Baggins\",\n" +
              "        \"extract\": \"Frodo Baggins is a fictional character in J. R. R. Tolkien's legendarium, and the main protagonist of The Lord of the Rings. Frodo is a hobbit of the Shire who inherits the One Ring from his cousin Bilbo Baggins and undertakes the quest to destroy it in the fires of Mount Doom. He is also mentioned in Tolkien's posthumously published works, The Silmarillion and Unfinished Tales.\"\n" +
              "      }\n" +
              "    }\n" +
              "  }\n" +
              "}";
Root root = new Gson().fromJson(json, Root.class);
for (Page page : root.query.pages.values()) {
    System.out.println(page.title);
    System.out.println("  " + page.extract);
}

输出

Frodo Baggins
  Frodo Baggins is a fictional character in J. R. R. Tolkien's legendarium, and the main protagonist of The Lord of the Rings. Frodo is a hobbit of the Shire who inherits the One Ring from his cousin Bilbo Baggins and undertakes the quest to destroy it in the fires of Mount Doom. He is also mentioned in Tolkien's posthumously published works, The Silmarillion and Unfinished Tales.

相关问题