0

In controller side i am 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 this is String. I am trying to parse this. Through

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"}'):

What i am doing wrong here. Any suggestions?

2 Answers 2

4

It's not valid json, this will 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 you could use: http://www.jslint.com/. And basic json structure: http://json.org/

UPDATED

In your case just try this:

JSON.parse('[' + params_gotton + ']')
Sign up to request clarification or add additional context in comments.

5 Comments

Mind blowing!! what the thing turned you on to think this way..I was keep trying some rubbish things in my editor.. :(
I know if inside array this will work. But there is no array in my param. Then how can i parse. My param is "{\"violation_date\":\"sdfsdf\",\"violation_time\":\"\"},{\"violation_date\":\"sdfdsf\",\"violation_time\":\"sdfsdf\"},{\"violation_date\":\"1233\",\"violation_ time\":\"\"},{\"violation_date\":\"test\",\"violation_time\":\"time\"}"
So add [ and ] to the beginning and end of the string of parameters. '[' + params_gotton + ']' should work
I tried to add through params_gotton.gsub(params_gotton.chars.first,'[{').gsub(params_gotton[-1,1],'}]') but i got response as "[{\"violation_date\":\"sdfsdf\",\"violation_time\":\"\"}],[{\"violation_date\":\"sdfdsf\",\"violation_time\":\"sdfsdf\"}],[{\"violation_date\":\"1233\",\"viola tion_time\":\"\"}],[{\"violation_date\":\"test\",\"violation_time\":\"time\"}]" This is also not working
Thanks a lot Mr.Yevgeniy Anfilofyev. I spent more time to parse this. Thank u so much.
0

well, received string does not contain a proper Json structure..

First convert that received param in a proper json structure and then parse it using "JSON.parse(params_gotton)".

In above received data all key and value shud be in a key value pair string format.. remove "\" symbol from received data..

it will definitely work fine..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.