开发者

Filtering the result of a JSON.parse response in Ruby (and the JSON gem)

开发者 https://www.devze.com 2022-12-11 06:02 出处:网络
In a little app I\'m building, I\'m using the the twitter_oauth gem (source of the methods I\'m using), which incidentally means I\'m dealing with the JSON ruby gem.

In a little app I'm building, I'm using the the twitter_oauth gem (source of the methods I'm using), which incidentally means I'm dealing with the JSON ruby gem.

I'm using the messages method, whose source is as follows:

def messages(page=1)
      oauth_response = access_token.get("/direct_messages.json?page=#{page}")
      JSON.parse(oauth_response.body)
end

It parses the JSON produced by Twitter, using the JSON.parse method. Now, what I want to do is filter the response, so as to show only the messages sent by a certain user. In other words, I want to be able to get an accounts message's on a per user 开发者_如何学编程basis.

I went through the JSON gem docs, but couldn't find an easy way to do this. I normally don't work with JSON (I prefer XML), but since the Twitter_oauth gem relies on it, I'm forced to learn it (unless I change the gem's source code or overwrite it - not of my preference).

Does any one know a pragmatic way of sorting JSON in Ruby?


Why do you have to work with JSON at all? JSON.parse gives you back deserialized structures (Ruby arrays and hashes), so all you have to do (I presume, you haven't pasted sample JSON output here) is to do select on whatever output JSON.parse (or, in your case, messages) method returns.

0

精彩评论

暂无评论...
验证码 换一张
取 消