QA@IT

How to get data by curb gem better?

2234 PV

I use curb gem to get some data from other website.

When I try

http = Curl.get("http://www.somesite.com/")
puts http.body_str

The data were

"{\"data\":[{\"id\":\"10000123456789\",\"name\":\"\\u1234\\u1234 \\u1234\\u1234\"},{\"id\":\"10000123465789\",\"name\":\"\\u1234\\u1234 \\u1234\\u1234\"},...}}}"

How to get data or id or name?

回答

If you get json always, try following code:

require 'curb'
require 'json'

http = Curl.get("http://www.somesite.com/")
puts http.body_str

val = JSON.parse(http.body_str)
p val["data"]
p val["data"][0]["id"]
編集 履歴 (0)
ウォッチ

この質問への回答やコメントをメールでお知らせします。