ruby - Issue on parsing string at JSON.parse -
in controller side getting params like
"{\"violation_date\":\"sdfsdf\",\"violation_time\":\"\"},{\"violation_date\":\"sdfdsf\",\"violation_time\":\"sdfsdf\"},{\"violation_date\":\"1233\",\"violation_ time\":\"\"},{\"violation_date\":\"test\",\"violation_time\":\"time\"}"
class of string
. trying parse this. through
json.parse(params_gotton)
got
json::parsererror (757: unexpected token @ ',{"violation_date":"sdfdsf","violation_time":"sdfsdf"},{"violation_date":"1233","violation_time":""},{"violation_d te":"test","violation_time":"time"}'):
what doing wrong here. suggestions?
it's not valid json, work (use []):
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"}]
to verify json string use: http://www.jslint.com/. , basic json structure: http://json.org/
updated
in case try this:
json.parse('[' + params_gotton + ']')
Comments
Post a Comment