jacobt
jacobt

Reputation: 157

array of strings to array of objects

My model is giving me an array of strings as json, and I need an array of objects for my api to function correctly on the client side. In my model I define the json as

def as_json(options={})
    super(:only => [:price, :available, :location_id, :beer_id], :methods => [:name, :brewery, :style, :label_url, :description, :id])
  end

This is giving me a response

{"available":"on","beer_id":1,"created_at":"2013-05-31T16:45:09Z","description":"Good","id":1,"location_id":1,"price":"3.0","size":"16.0","updated_at":"2013-05-31T16:45:09Z"}

which is obviously missing the [ ] indicating it is an array of objects. Is there a simple way to convert this array of strings into an array of objects?

EDIT: My original response which was working fine was

[{"available":"on","beer_id":1,"created_at":"2013-05-31T16:45:09Z","description":"Good","id":1,"location_id":1,"price":"3.0","size":"16.0","updated_at":"2013-05-31T16:45:09Z"}]

My ios app is crashing because it doesn't think the current response is of type NSDictionary because it is NSString. After looking around it appears that some change I made to the rails app changed the json response. The only clue I have is the missing brackets when I look at what was working and what is now not working.

Upvotes: 0

Views: 122

Answers (1)

Sergey Bolgov
Sergey Bolgov

Reputation: 806

As far as I can see, your response is not an array of strings, it's just a single JSON object.

If you need to convert the object into 1-element array of objects, just enclose it into [ and ]. :)

Upvotes: 1

Related Questions