2013-10-28 85 views
0

我正在使用HTTParty來調用Flickr API。我得到一個迴應,但它逃脫了。我怎樣才能簡單明瞭的JSON>標準化json

class Flickr 

    require 'httparty' 
    include HTTParty 

    API_KEY   = '2e18b933c285b552701e7c6f5bff3aee' 
    SECRET   = '88123767bb4492b2' 
    USER_ID   = '[email protected]' 
    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 \「,\」icon \「\」,\「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 \」:\「Paris Trip \ ,「description」:「2008年5月11日至13日 - (Peeps)\」},{\「id \」:\「72157629097324306 \」,\「title \」:\「Ohio \ 「:」8月2日005 - (Peeps)\「},{\」id \「:\」72157629097297760 \「,\」title \「:\」Iceland Holiday \「,\」description \「:」2004年8月5日至16日 - Peeps)\「}]},{\」id \「:\」57928212-236572157629691182748 \「,\」title \「:\」1 - Trip \「,\」description \「:\」\「 :\「\/images \ /collection_default_l.gif \」,\「iconsmall \」:\「\/images \ /collection_default_s.gif \」,\「set \」:[{\「id \」: 「72157629683532365562」,\「title \」:\「Day 1 \」,\「description \」:\「\」},{\「id \」:\「72157629683246523541696 \」,\「title \ 「Day 2」,「description」:\「\」},{\「id \」:\「7215762968234563554852 \」,\「title \」:\「Day 3 \ :\「id」:\「7215762972346510465912 \」,\「title \」:\「Day 4 \」,\「description \」:\「\」},{\「id \ 「:\」72157362629710474644 \「,\」title \「:\」Day 5 South \「,\」description \「:\」\「},{\」id \「:\」7215762923564710489442 \「,\」title \「:\」Day 6 \「,\」description \「:\」\「},{\」id \「:\」721576292346834618028 \「,\」title \「:\」Day 7 i \ 「description \」:\「\」},{\「id \」:\「721576265349834637138 \」,\「title \」:\「Day 8 \」,\「description \」:\「\」},{ \ 「ID \」:\「7215762 9236836419418「,\」title \「:\」第9天\「,\」description \「:\」\「},{\」id \「:\」7215762985641123596 \「,\」title \「:\」第10天「,」描述「:」\「},{\」id \「:\」72157629854631700916 \「,\」title \「:\」Day 11 & 12 \「,\」description \ :\「\」},{\「id \」:\「721576223649851713058 \」,\「title \」:\「Day 13,14 & 15 - \」,\「description \」:\「\」}] :\「57928212-72236157629866347760 \」,\「title \」:\「2 - Trip 2010 \」,\「description \」:\「\」,\「iconlarge \」: 「\/images \ /collection_default_l.gif \」,\「iconsmall \」:\「\/images \ /collection_default_s.gif \」,\「set \」:[{\「id \」:\「721576292346866337538 \」 ,\ 「稱號\」:\「0天 - 到達....

+0

可以考慮看看:https://github.com/hoxworth/json-schema不知道這是否是你會在之後 – David

+0

'to_json'和'JSON.parse'或多或少是逆操作。我不確定你想要做什麼。 「適當的json」是什麼意思? – iltempo

+0

我不認爲這就是我所追求的。我希望將我的回覆變成有效的json。據我所知,json不支持方括號和散列箭頭「=>」。 – rctneil

回答

1

response.to_json會給你正確的JSON。 JSON.parse(response.to_json)會將該JSON轉換爲散列/數組/等的紅寶石散列。

+0

啊好吧。當我做response.to_json。很多單引號和雙引號都被轉義了。這怎麼解決?一旦我有了沒有轉義字符的有效json,循環這個結果集的最好方法是什麼? – rctneil

+0

你確定他們已經逃脫了嗎?如果你在控制檯中這樣做可能會發生,但它僅用於顯示。至於循環它..真的取決於什麼數據被返回。 –

+0

我會更新我的OP現在顯示從Flickr返回的json是在response.body – rctneil