我在响应中返回了以下JSON:
"{\n \"notices\": [\n {\n \"id\": \"1234\",\n \"projectId\": 1,\n \"groupId\": \"55\",\n \"createdAt\": \"2014-10-22T03:00:00.407Z\"\n },\n {\n \"id\": \"1234\",\n \"projectId\": 1,\n \"groupId\": \"55\",\n \"createdAt\": \"2014-10-22T03:00:00.407Z\"\n },\n ],\n \"count\": 2\n}\n"
在Ruby 2.3.0的irb中,当我在上面的响应中使用JSON.parse(x)
时,我得到:
JSON::ParserError: 419: unexpected token at '],
"count": 2
}'
尽管http://jsonlint.com表示它是有效的JSON。我做错了什么?
答案 0 :(得分:2)
在数组中的最后一个对象之后使用逗号似乎存在问题:
\"createdAt\": \"2014-10-22T03:00:00.407Z\"\n },\n ],\n \"count\": 2\n}\n"
^ This comma
在摆脱它之后,我得到了解析按预期工作:
JSON.parse("{\n \"notices\": [\n {\n \"id\": \"1234\",\n \"projectId\": 1,\n \"groupId\": \"55\",\n \"createdAt\": \"2014-10-22T03:00:00.407Z\"\n },\n {\n \"id\": \"1234\",\n \"projectId\": 1,\n \"groupId\": \"55\",\n \"createdAt\": \"2014-10-22T03:00:00.407Z\"\n } ],\n \"count\": 2\n}\n")
=> {"notices"=>[{"id"=>"1234", "projectId"=>1, "groupId"=>"55", "createdAt"=>"2014-10-22T03:00:00.407Z"}, {"id"=>"1234", "projectId"=>1, "groupId"=>"55", "createdAt"=>"2014-10-22T03:00:00.407Z"}], "count"=>2}