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 + ']') 
    5
    • Mind blowing!! what the thing turned you on to think this way..I was keep trying some rubbish things in my editor.. :(CommentedSep 4, 2013 at 11:40
    • 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\"}"
      – vinothini
      CommentedSep 4, 2013 at 11:42
    • 1
      So add [ and ] to the beginning and end of the string of parameters. '[' + params_gotton + ']' should workCommentedSep 4, 2013 at 11:43
    • 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
      – vinothini
      CommentedSep 4, 2013 at 11:46
    • Thanks a lot Mr.Yevgeniy Anfilofyev. I spent more time to parse this. Thank u so much.
      – vinothini
      CommentedSep 4, 2013 at 11:53
    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..

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.