Android GSON:需要一个字符串,但是是 BEGIN_OBJECT?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11571412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:05:04  来源:igfitidea点击:

GSON: Expected a string but was BEGIN_OBJECT?

androidjsongson

提问by Steven Schoen

I'm trying to use GSON to parse some very simple JSON. Here's my code:

我正在尝试使用 GSON 来解析一些非常简单的 JSON。这是我的代码:

    Gson gson = new Gson();

    InputStreamReader reader = new InputStreamReader(getJsonData(url));
    String key = gson.fromJson(reader, String.class);

Here's the JSON returned from the url:

这是从 url 返回的 JSON:

{
  "access_token": "abcdefgh"
}

I'm getting this exception:

我收到此异常:

E/AndroidRuntime(19447): com.google.gson.JsonSyntaxException:     java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2

Any ideas? I'm new to GSON.

有任何想法吗?我是 GSON 的新手。

回答by Programmer Bruce

The JSON structure is an object with one element named "access_token" -- it's not just a simple string. It could be deserialized to a matching Java data structure, such as a Map, as follows.

JSON 结构是一个对象,其中包含一个名为“access_token”的元素——它不仅仅是一个简单的字符串。它可以反序列化为匹配的 Java 数据结构,例如 Map,如下所示。

import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonFoo
{
  public static void main(String[] args)
  {
    String jsonInput = "{\"access_token\": \"abcdefgh\"}";

    Map<String, String> map = new Gson().fromJson(jsonInput, new TypeToken<Map<String, String>>() {}.getType());
    String key = map.get("access_token");
    System.out.println(key);
  }
}

Another common approach is to use a more specific Java data structure that matches the JSON. For example:

另一种常见的方法是使用与 JSON 匹配的更具体的 Java 数据结构。例如:

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

public class GsonFoo
{
  public static void main(String[] args)
  {
    String jsonInput = "{\"access_token\": \"abcdefgh\"}";

    Response response = new Gson().fromJson(jsonInput, Response.class);
    System.out.println(response.key);
  }
}

class Response
{
  @SerializedName("access_token")
  String key;
}

回答by giampaolo

Another "low level" possibility using the Gson JsonParser:

使用 Gson JsonParser 的另一种“低级”可能性:

package stackoverflow.questions.q11571412;

import com.google.gson.*;

public class GsonFooWithParser
{
  public static void main(String[] args)
  {
    String jsonInput = "{\"access_token\": \"abcdefgh\"}";

    JsonElement je = new JsonParser().parse(jsonInput);

    String value = je.getAsJsonObject().get("access_token").getAsString();
    System.out.println(value);
  }
}

If one day you'll write a custom deserializer, JsonElement will be your best friend.

如果有一天您要编写自定义解串器,那么 JsonElement 将是您最好的朋友。