【问题标题】:Having trouble parsing JSON response from Wikipedia解析来自 Wikipedia 的 JSON 响应时遇到问题
【发布时间】:2017-01-04 01:23:23
【问题描述】:

所以我正在制作一个搜索 mediawiki api 以获取有关某些名人的简短信息的 Android 应用程序。

这个想法是您可以输入任何名称,它会提供 mediawiki api 关于该名称/人的信息,但现在我只是坚持使用一个名称,直到我弄清楚如何正确解析 JSON。

我需要从这个 json 响应中提取字段: JSON response

这是我目前所拥有的,我认为问题出在 Query 类中,我只是不知道该类需要什么来确保只返回提取字段。当我运行程序时,onResponse() 方法的输出为空。感谢您的帮助。

好的,我做了建议的更改,这是我更新的代码:

;

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

}

【问题讨论】:

  • 什么不起作用?也发布您的日志
  • 代码完全运行,日志中没有错误,问题是我没有为 JSON 数据中的每个对象设置类。我进行了 Andreas 建议的更改,并更新了上面的代码,但是我仍然没有做正确的事情,onResponse 的输出仍然是 NULL

标签: java android json gson


【解决方案1】:

我认为问题出在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 个字段:batchcompletequery

您尝试解析为具有以下字段的对象:extracttitle

你看到那里的差异吗?

如果你想使用 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.

【讨论】:

  • 好的,感谢您解决这个问题!我添加了您建议的类,这是否意味着在 onResponse 方法中我应该像这样获取提取字符串? public void onResponse(String response) { final TextView textView = (TextView) findViewById(R.id.textView); Page page = gson.fromJson(response, Page.class); textView.setText(page.extract); }
  • 因为我运行了该程序,但在我进行这些更改后它仍然输出为 null。你绝对是对的,我只是觉得我还是做错了。
  • 输出不再是 NULL,它现在输出一个空白字符串,或者什么都没有。我认为它输出字符串提取,但它似乎是空的。
  • 我说“根对象有2个字段:batchcompletequery。然后我向你展示了一个名为Root 的类,其中包含这两个字段。因此,将 JSON 文本解析到该类中,以便它可以正确映射字段。这么难理解吗?
  • 好的,我明白了。无需居高临下,感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多