JSON 端点的数据格式

JSON Data Format At End Point

工具:Spring 数据 JPS,Spring 数据休息,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 数据类型格式是什么?

您的第一个示例无效仅仅是因为您引用地址值的方式。如果您删除单引号,它将正确地格式化 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\"}"

}