2013/03/07

JSONをJSONPに変換するYahoo PipesのAPI

もはやなんのことやらわからないかもしれないが、JSONPのコールバック指定に対応していないJSON形式のAPIを、JavaScriptからも呼び出せるように、JSONPに変換してくれるAPIが、Yahoo Pipesにあったので紹介します。

API仕様

この JSON to JSONP bridgeAPI を使います。パラメータとして渡すJSONのURLは、エンコードが必要なので注意してください。

http://pipes.yahoo.com/pipes/pipe.run?u=JSONを返すAPIのURL&_id=332d9216d8910ba39e6c2577fd321a6a&_render=json&_callback=コールバック名

レスポンス例

Livedoor Weather Hacksの天気予報APIを使って、JSONP変換してみます。以下は、オリジナル(JSON)のレスポンス。publiTtimeって一体なんだろうか...。
{
 "publiTtime":"2013-03-06T17:00:00\u002b0900",
 "title":"\u9577\u91ce\u770c \u9577\u91ce \u306e\u5929\u6c17",
 "description":{
  "text":" \u65e5\u672c\u306e\u5357\u306b\u306f\u9ad8\u6c17...",
  "publicTime":"2013-03-06T16:40:00\u002b0900"
 },
 "link":"http://weather.livedoor.com/area/forecast/200010",
 "pinpointLocations":[
  {
   "link":"http://weather.livedoor.com/area/forecast/2020100",
   "name":"\u9577\u91ce\u5e02"
  },
  ...
 ],
 ...
}

これが、Yahoo Pipsを使って、JSONP変換するとこんな感じになります。Yahoo Pipsのヘッダー情報などが入り、元のデータがネストするので、注意してください。value.items 以下が元のデータです。
a({
 "count":1,
 "value":
 {
  "title":"JSON to JSONP bridge",
  "description":"To convert any JSON delivering url to JSONP, fetch \"http:\/\/pipes.yahoo.com\/pipes\/pipe.run?u=\"+ encodeURIComponent(url) + \"&_id=332d9216d8910ba39e6c2577fd321a6a&_render=json&_callback=\" + callback name",
  "link":"http:\/\/pipes.yahoo.com\/pipes\/pipe.info?_id=332d9216d8910ba39e6c2577fd321a6a",
  "pubDate":"Wed, 06 Mar 2013 12:20:46 +0000",
  "generator":"http:\/\/pipes.yahoo.com\/pipes\/",
  "callback":"a",
  "items":[
   {
    "publiTtime":"2013-03-06T17:00:00+0900",
    "title":"\u9577\u91ce\u770c \u9577\u91ce \u306e\u5929\u6c17",
    "description":{
     "text":"\u65e5\u672c\u306e\u5357\u306b\u306f\u9ad8....",
     "publicTime":"2013-03-06T16:40:00+0900"
    },
    "link":"http://weather.livedoor.com/area/forecast/200010",
    "pinpointLocations":[
     {
      "link":"http:\/\/weather.livedoor.com\/area\/forecast\/2020100",
      "name":"\u9577\u91ce\u5e02"
     },
     ...
    ]
   }
  ]
 }
})

まとめ

Yahoo Pipesのヘッダーが結構ウザイですね。素の形式で変換してくれるヤツをまた探しておきます。話は変わりますが、publiTtimeってなんだろうかー。


0 件のコメント:

コメントを投稿