无法解析来自*的JSON响应

时间:2021-01-15 22:25:41

So I'm making an android app that searches the mediawiki api to get a short paragraph of information about certain famous people.

因此,我正在制作一个搜索mediawiki api的Android应用程序,以获取有关某些名人的一小段信息。

The idea is that you'll be able to enter any name and it will provide the information that the mediawiki api has on that name/person, but for now I'm just sticking with one name until I figure out how to parse the JSON correctly.

这个想法是你将能够输入任何名称,它将提供mediawiki api对该名称/人的信息,但是现在我只是坚持一个名称,直到我弄清楚如何解析JSON正确。

I need the extract field from this json response: JSON response

我需要来自这个json响应的提取字段:JSON响应

This is currently what I have, I think the problem is within the Query class, I just don't know what that class needs to make sure only the extract field is returned. When I run the program the output of the onResponse() method is just null. Thanks for any help.

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

Okay I made the changes that were suggested and this is my updated code:

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

;

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

}

}

1 个解决方案

#1


4  

I think the problem is within the Query class

我认为问题出在Query类中

You are correct.

你是对的。

The JSON data looks like this:

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."
      }
    }
  }
}

The root object has 2 fields: batchcomplete and query.

根对象有2个字段:batchcomplete和query。

You try to parse to an object with these fields: extract and title.

您尝试使用以下字段解析对象:extract和title。

Do you see the discrepancy there?

你看到那里的差异吗?

You need classes for all the objects, if you want to use Gson.

如果要使用Gson,则需要所有对象的类。

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

UPDATE

UPDATE

You need to parse the JSON into the Root object.

您需要将JSON解析为Root对象。

Example code:

示例代码:

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

Output

产量

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.

#1


4  

I think the problem is within the Query class

我认为问题出在Query类中

You are correct.

你是对的。

The JSON data looks like this:

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."
      }
    }
  }
}

The root object has 2 fields: batchcomplete and query.

根对象有2个字段:batchcomplete和query。

You try to parse to an object with these fields: extract and title.

您尝试使用以下字段解析对象:extract和title。

Do you see the discrepancy there?

你看到那里的差异吗?

You need classes for all the objects, if you want to use Gson.

如果要使用Gson,则需要所有对象的类。

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

UPDATE

UPDATE

You need to parse the JSON into the Root object.

您需要将JSON解析为Root对象。

Example code:

示例代码:

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

Output

产量

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.