GSON无法解析带有字符串空间的JSON [英] GSON fails parsing JSON with string space

查看:116
本文介绍了GSON无法解析带有字符串空间的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下对象:

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包含带空格的字符串时,我看到以下异常:

I am serializing it into JSON using the Gson library. When value contains a string with no spaces, it works perfectly. When value contains a string with spaces, I see the following exception:

com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:第1行第28列的未终止对象

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,这很好.

The JSON is parsed successfully. Is this a known issue? I've ran the JSON through a validator and it is perfectly fine.

这个问题的模糊性并没有被忽视.我希望这是一个现有问题.该应用程序非常庞大,这就是为什么很难举起一个小的,可编译的示例,但是我会尽我所能!

The vagueness of the question wasn't unnoticed. I was hoping this was an existing issue. The application is huge, which is why prying out a small, compilable example is difficult, but I'll do what I can!

好的.因此,首先,将以下JSON发送到我的控制器:

OKAY. So firstly, the below JSON is send to my controller:

@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)时,它会移至该方法:

On the call to parseAlgorithmState(state), it moves down to this method:

@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);行是最初发生异常的地方.

The line Map keyValueMap = (Map) gson.fromJson(json.trim(), Object.class); is where the exception is initially occurring.

推荐答案

我刚刚遇到了同样的问题.问题在于该字符是一个不间断的字符char(160).如果使用POSTMAN将其粘贴到请求的正文中,则可以直观地看到字符(它们看起来像灰色的点).吉森不喜欢他们.

I just had this same issue. The problem is that the character is a non-breaking space, char(160). If you paste this in the body of a request using POSTMAN, you'll visually be able to see the characters (They look like gray dots). Gson doesn't like them.

这篇关于GSON无法解析带有字符串空间的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆