繁体   English   中英

GSON 无法使用字符串空间解析 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:第 1 行第 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)使解析器的限制更少。

查看文档,您的 Json 字符串中不存在setLenient(true)禁用的所有限制,除了第一个(这可能是问题,因为您是从网络服务器获取字符串)。

我刚刚遇到了同样的问题。 问题是字符是一个不间断的空格,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”}

解析器指向第一个空格之后的字符。

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

希望这对未来的人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM