GSON 无法用字符串 space 解析 JSON

GSON fails parsing JSON with string space

我有以下对象:

public class ParameterWrapper<T> {

    private String type;

    private T value;

    public ParameterWrapper(String type, T value) {
        this.type = type;
        this.value = value;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}

我正在使用 Gson 库将其序列化为 JSON。当 value 包含一个没有空格的字符串时,它工作得很好。当 value 包含带空格的字符串时,我看到以下异常:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 28

以下JSON:

{"Plaintext":{"type":"String","value":"hello there"},"Key":{"type":"Number","value":"1"},"SINGLE_FUNCTION":{"value":"1-0"}}

但是以下内容:

{"Plaintext":{"type":"String","value":"hellothere"},"Key":{"type":"Number","value":"1"},"SINGLE_FUNCTION":{"value":"1-0"}}

JSON解析成功。这是一个已知的问题?我已经通过验证器 运行 JSON 了,完全没问题。

编辑:

问题的含糊之处并没有被忽视。我希望这是一个存在的问题。应用程序很大,这就是为什么撬出一个小的、可编译的例子很困难的原因,但我会尽力而为!

好的。所以首先,下面的 JSON 被发送到我的控制器:

@RequestMapping("/update-state")
public
@ResponseBody
String updateState(@RequestParam(value = "algorithmId") String algorithmId,
                   @RequestParam(value = "state") String state) throws InvalidParameterException {

    Algorithm algorithm = algorithmService.getAlgorithmById(Integer.parseInt(algorithmId));
    algorithm.execute(executorService.parseAlgorithmState(state));

    return gsonBuilder.toJson(algorithm.getState().getParameterMap());
}

在调用 parseAlgorithmState(state) 时,它向下移动到此方法:

@Override
public AlgorithmState parseAlgorithmState(String json) throws InvalidParameterException {
    Gson gson = new Gson();
    Map keyValueMap = (Map) gson.fromJson(json.trim(), Object.class);
    ...

Map keyValueMap = (Map) gson.fromJson(json.trim(), Object.class); 行是异常最初发生的地方。

所以经过一番讨论,以下解决方法奏效了:

Gson gson = new Gson(); 
JsonReader jr = new JsonReader(new StringReader(s.trim())); 
jr.setLenient(true); 
Map keyValueMap = (Map) gson.fromJson(jr, Object.class);

setLenient(true) 使解析器的限制少一些。

查看文档,setLenient(true) 禁用的每个限制都不存在于您的 Json 字符串中,除了 first one(这可能是问题,因为您正在获取字符串来自网络服务器)。

我刚遇到同样的问题。问题是字符是不间断的 space, char(160)。如果您使用 POSTMAN 将其粘贴到请求正文中,您将能够直观地看到这些字符(它们看起来像灰点)。 Gson 不喜欢他们。

这对我有用

  Gson gson = new Gson(); 
  Map keyValueMap = (Map) gson.fromJson(gson.toJson(s.trim()), Object.class);

我遇到了同样的错误 JSON:

{“id”:“”,“title”:“Hello SiteCore”,“description”:“This is the body text from SiteCore”,“type”:“Information”,“displayType”:“Banner”}

解析器指向第一个白色 space 之后的字符。

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 26 path $.null

经过大量搜索并多了几双眼睛,我们发现 JSON(来自 Sitecore 数据库)是用 而不是 " 创建的。

将所有 个字符更改为 " 使用常用的 Gson 代码解决了我的问题:

    @JvmStatic
    fun convertFromJsonString(data: String): Message? {
        if (data.isEmpty())
            return null
        return Gson().fromJson(data, Message::class.java)
    }

希望这对以后的人有所帮助。