我刚刚开始使用Jackson,因为它与Spring Framework集成,并且遇到了一个值为单引号的问题.当尝试在页面上使用jQuery解析JSON时,我收到一个JavaScript错误"SyntaxError: missing ) after argument list".我习惯使用Gson来序列化我的对象,并且不会遇到这个问题,因为Gson会用Unicode\u0027替换单引号.
例如;
Java的
public final class Person {
private String firstName;
private String lastName;
public Person() {}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
}
Run Code Online (Sandbox Code Playgroud)
杰森
[{"person":{"firstName":"James","lastName":"O'tool"}}]
在
杰森的
JSON
[{"person":{"firstName":"James","lastName":"O\u0027tool"}}]
JavaScript ;
// This is where the JavaScript fails with the Jackson serialized object …