【发布时间】:2013-10-28 22:28:35
【问题描述】:
我正在使用 HTTParty 调用 Flickr API。我得到了回应,但它已经逃脱了。如何获得简单明了的 json?>
class Flickr
require 'httparty'
include HTTParty
API_KEY = '2e18b933c285b552701e7c6f5bff3aee'
SECRET = '88123767bb4492b2'
USER_ID = '57951266@N03'
RESPONSE_FORMAT = 'json'
base_uri 'http://api.flickr.com'
# Get list of all collections
def self.collections_list
response = get("/services/rest/",
:query => {
method: 'flickr.collections.getTree',
api_key: API_KEY,
user_id: USER_ID,
format: RESPONSE_FORMAT,
nojsoncallback: 1
}
)
response.body
end
这是response.body 返回的内容:如何禁用阻止它成为有效 json 的转义?
=> "{\"collections\":{\"collection\":[{\"id\":\"579244212-72157632934521433434033423\", \"title\":\"mysite.co.uk\ ", \"说明\":\"\", \"iconlarge\":\"\/images\/collection_default_l.gif\", \"iconsmall\":\"\/images\/collection_default_s.gif\" , \"collection\":[{\"id\":\"5723456928212-72157634949706778\", \"title\":\"Holidays\", \"description\":\"\", \"iconlarge\ ":\"\/images\/collection_default_l.gif\", \"iconsmall\":\"\/images\/collection_default_s.gif\", \"collection\":[{\"id\":\" 5792364528212-7215763492346543457291\", \"title\":\"Standard\", \"description\":\"\", \"iconlarge\":\"\/images\/collection_default_l.gif\", \" iconsmall\":\"\/images\/collection_default_s.gif\", \"set\":[{\"id\":\"72157628982366181735\", \"title\":\"巴黎之旅\", \"description\":\"11th-13th May 2008 - (Peeps)\"}, {\"id\":\"72157629097324306\", \"title\":\"Ohio\", \"description\ ":\"2005 年 8 月 - (Peeps)\"}, {\"id\":\"72157629097297760\", \"title\":\"Iceland Holiday\", \"description\":\"5th- 2004 年 8 月 16 日 - (窥视)\"}]}, {\"id\":\"57928212-236572157629691182748\", \"title\":\"1 - Trip\", \"description\":\"\", \" iconlarge\":\"\/images\/collection_default_l.gif\", \"iconsmall\":\"\/images\/collection_default_s.gif\", \"set\":[{\"id\": \"72157629683532365562\", \"title\":\"Day 1\", \"description\":\"\"}, {\"id\":\"72157629683246523541696\", \"title\": \"第 2 天\", \"描述\":\"\"}, {\"id\":\"7215762968234563554852\", \"标题\":\"第 3 天\", \"描述\" :\"\"}, {\"id\":\"7215762972346510465912\", \"title\":\"第 4 天\", \"描述\":\"\"}, {\"id\ ":\"72157362629710474644\", \"title\":\"Day 5 South\", \"description\":\"\"}, {\"id\":\"7215762923564710489442\", \"title \":\"第 6 天 \", \"描述\":\"\"}, {\"id\":\"721576292346834618028\", \"title\":\"第 7 天 i\", \ "说明\":\"\"}, {\"id\":\"721576265349834637138\", \"title\":\"第 8 天\", \"说明\":\"\"}, { \"id\":\"72157629236836419418\", \"title\":\"第 9 天\", \"description\":\"\"}, {\"id\":\"7215762985641123596\", \"title\":\"第 10 天\", \"d说明\":\"\"}, {\"id\":\"72157629854631700916\", \"title\":\"第 11 & 12 天\", \"说明\":\"\"}, {\"id\":\"721576223649851713058\", \"title\":\"Day 13,14 & 15 -\", \"description\":\"\"}]}, {\"id\ ":\"57928212-72236157629866347760\", \"title\":\"2 - Trip 2010\", \"description\":\"\", \"iconlarge\":\"\/images\/collection_default_l .gif\", \"iconsmall\":\"\/images\/collection_default_s.gif\", \"set\":[{\"id\":\"721576292346866337538\", \"title\": \"第 0 天 - 到达 ....
【问题讨论】:
-
可以考虑看看:github.com/hoxworth/json-schema 不确定这是否是您想要的结果
-
to_json和JSON.parse或多或少是逆运算。我不确定您要做什么。 “正确的 json”是什么意思? -
我不认为这就是我所追求的。我希望将我的回复转换为有效的 json。据我所知,json 不支持方括号和散列箭头“=>”。
标签: ruby-on-rails json api httparty