Thursday, February 8, 2018

Convert JSON String to JSON Array in rails?


You can use the json library json
You can then do:
jsonArray = [{"content":"1D","createdTime":"09-06-2011 00:59"},   
              {"content":"2D","createdtime":"09-06-2011 08:00"}]
objArray = JSON.parse(jsonArray)
In response to your comment, you can do this, as long as your JSON fits your model
objArray.each do |object|
  # This is a hash object so now create a new one.
  newMyObject = MyObject.new(object)
  newMyObject.save # You can do validation or any other processing around here.
end

-------------------


ActiveSupport::JSON.decode(string)
 will decode that for you into a delicious consumable object on the server side.

No comments:

Post a Comment