在控制器方面,我正在获得像
这样的参数"{\"violation_date\":\"sdfsdf\",\"violation_time\":\"\"},{\"violation_date\":\"sdfdsf\",\"violation_time\":\"sdfsdf\"},{\"violation_date\":\"1233\",\"violation_
time\":\"\"},{\"violation_date\":\"test\",\"violation_time\":\"time\"}"
这个类是String
。我试图解析这个。通过
JSON.parse(params_gotton)
GOT
JSON::ParserError (757: unexpected token at ',{"violation_date":"sdfdsf","violation_time":"sdfsdf"},{"violation_date":"1233","violation_time":""},{"violation_d
te":"test","violation_time":"time"}'):
我在这里做错了什么。有什么建议吗?
答案 0 :(得分:4)
这是无效的json,这将起作用(使用[]):
require 'json'
jsn = '[{"violation_date":"sdfsdf","violation_time":""},
{"violation_date":"sdfdsf","violation_time":"sdfsdf"},
{"violation_date":"1233","violation_time":""},
{"violation_date":"test","violation_time":"time"}]'
JSON.parse(jsn) # => [{"violation_date"=>"sdfsdf", "violation_time"=>""}, {"violation_date"=>"sdfdsf", "violation_time"=>"sdfsdf"}, {"violation_date"=>"1233", "violation_time"=>""}, {"violation_date"=>"test", "violation_time"=>"time"}]
要验证json字符串,您可以使用:http://www.jslint.com/。 基本的json结构:http://json.org/
已更新
在你的情况下,试试这个:
JSON.parse('[' + params_gotton + ']')
答案 1 :(得分:0)
好吧,收到的字符串不包含正确的Json结构..
首先在正确的json结构中转换接收到的param,然后使用“JSON.parse(params_gotton)”解析它。
在上面收到的数据中,所有键值和值都是键值对字符串格式..从接收到的数据中删除“\”符号..
它肯定会正常工作..