端点的JSON数据格式

时间:2016-07-07 00:28:39

标签: postgresql spring-data-jpa

工具:Spring Data JPS,Spring Data Rest,PostgreSQL

在创建自定义的Hibernate UserType后,我用POST方法测试代码。对于以下数据格式,

{"firstName" : "Joe", "lastName": "Cooper", "address" : '{"street": "Street 1", "city": "My town", "state": "CA", "zip-code": "91210", "country": "US"}'} 

我收到错误

com.fasterxml.jackson.core.JsonParseException: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')

当我有推荐的json格式时

{"firstName" : "Joe", "lastName": "Smith", "address" : "{\\"street\\": \\"Street 1\\", \\"city\\": \\"My Town\\", \\"state\\": \\"CA\\", \\"zip-code\\": \\"98003\\", \\"country\\": \\"US\\"}"}

我收到了另一个错误

 com.fasterxml.jackson.core.JsonParseException: Unexpected character ('s' (code 115)): was expecting comma to separate OBJECT entries

那么什么是正确的JSON数据类型格式?

1 个答案:

答案 0 :(得分:0)

您的第一个示例无效仅仅是因为您引用了地址的值。如果删除单引号,它将正确格式化JSON,并将地址字段作为数据结构本身。

{   "firstName": "Joe",
"lastName": "Cooper",
"address": { "street": "Street 1", "city": "My town", "state": "CA", "zip-code": "91210", "country": "US"}

}

或者,如果该字段确实应该是一个字符串,则需要使用双引号(如JSON所要求的那样)然后转义字符串中的每个其他双引号:

{   "firstName": "Joe",
"lastName": "Cooper",
"address": "{ \"street\": \"Street 1\", \"city\": \"My town\", \"state\": \"CA\", \"zip-code\": \"91210\", \"country\": \"US\"}"

}