我有以下对象:
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,效果非常好。
Edit:
这个问题的含糊之处并没有被忽视。我希望这是一个现有的问题。该应用程序非常庞大,这就是为什么很难找出一个小的、可编译的示例,但我会尽力而为!
好的。首先,以下 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);
是异常最初发生的地方。