Search code examples
jsonrubymotion

Using BubbleWrap to read JSON files


I have a backend to an IOS application. I am trying to read the data from my rails backend by using JSON. My bubbleWrap get request is as follows.

BW::HTTP.get("url_here/children/1.json") do |response|
   json = BW::JSON.parse response.body.to_str
   for line in json
     p line[:name]
   end
end

It doesn't bring any data back, it actually breaks my code. I can't find any documentation with an example of how to use REST from rubymotion/Bubblewrap and pull data back to my application.

Any help is appreciated.


Solution

  • Always check the response code before parsing the response body. You might

    BW::HTTP.get(url) do |response|
      if response.ok?
        data = BW::JSON.parse(response.body.to_str)
        # make sure you have an array or hash before you try iterating over it
        data.each {|item| p item}
      else
        warn "Trouble"
      end
    end
    

    Also make sure you sanity-check the JSON response vs your code's expectation. Perhaps the JSON is an array and not a hash?