11

curlからのJSON応答をプリティファイしようとしていますが、機能しません。

Narnia:~ vitaly$ curl -s https://api.vk.com/method/photos.getAlbums?uid=6015870&access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37 | python -m json.tool
[4] 8822
No JSON object could be decoded
Narnia:~ vitaly$ {"response":[{"aid":"109967643","thumb_id":"163950716","owner_id":"6015870","title":"9 мая 2010","description":"","created":"1273613868","updated":"1273695901","size":7},{"aid":"95299056","thumb_id":"135702541","owner_id":"6015870","title":"Kemer 2009","description":"http:\/\/picasaweb.google.com.ua\/Ivanov.Vitalii\/Kemer2009","created":"1250355164","updated":"1250520619","size":72},{"aid":"43368938","thumb_id":"116630327","owner_id":"6015870","title":"Лето 2008 (Алупка)","description":"","created":"1220478168","updated":"1221348162","size":43},{"aid":"38630587","thumb_id":"116886016","owner_id":"6015870","title":"Flowers","description":"","created":"1217680400","updated":"1236774230","size":9},{"aid":"36658103","thumb_id":"163954451","owner_id":"6015870","title":"Моя ката","description":"","created":"1216419744","updated":"1273698620","size":8},{"aid":"23100962","thumb_id":"112723283","owner_id":"6015870","title":"Только пипл","description":"","created":"1208636545","updated":"1210382181","size":9},{"aid":"15473894","thumb_id":"114370266","owner_id":"6015870","title":"other","description":"","created":"1203516879","updated":"1327679223","size":29},{"aid":"15471241","thumb_id":"95266020","owner_id":"6015870","title":"Сам батя или чего нельзя в фотошопе =)","description":"","created":"1203516081","updated":"1203516728","size":4}]}
[4]   Done                    curl -s https://api.vk.com/method/photos.getAlbums?uid=6015870
Narnia:~ vitaly$ 

「JSONオブジェクトをデコードできませんでした」と表示されるのはなぜですか?私が要求しているURLは常に有効なjsonを返します。$ echojsonreponse|のように手動で応答からjsonを渡す場合 python -m json.tool、jsonをきれいにします。

私は何か間違ったことをしていますか?

4

2 に答える 2

16

URLを引用符で囲む必要があります。

curl -s "https://api.vk.com/method/photos.getAlbums?uid=6015870&access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37" | python -m json.tool

&文字はシェルメタ文字であり、コマンドをバックグラウンドで実行します。その結果、完全なURLをサーバーに渡すのではなく、curl -s https://api.vk.com/method/photos.getAlbums?uid=6015870代わりにコマンドをバックグラウンドで実行します。

これが、シェルがエコー[4] 8822する理由です。これまでに4番目のジョブをジョブキューに入れると、PID8822が与えられました。

残りのコマンドは次のとおりです。

access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37 | python -m json.tool

これは実際には有効なJSONを生成しません:

$ access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37 | python -m json.tool
No JSON object could be decoded

次の行で、バックグラウンドに配置したジョブが完了したことを確認できます。

Narnia:~ vitaly$ {"response":[{"aid":"109967643","thumb_id":"163950716","owner_id":"6015870","title":"9 мая 2010","description":"","created":"1273613868","updated":"1273695901","size":7},{"aid":"95299056","thumb_id":"135702541","owner_id":"6015870","title":"Kemer 2009","description":"http:\/\/picasaweb.google.com.ua\/Ivanov.Vitalii\/Kemer2009","created":"1250355164","updated":"1250520619","size":72},{"aid":"43368938","thumb_id":"116630327","owner_id":"6015870","title":"Лето 2008 (Алупка)","description":"","created":"1220478168","updated":"1221348162","size":43},{"aid":"38630587","thumb_id":"116886016","owner_id":"6015870","title":"Flowers","description":"","created":"1217680400","updated":"1236774230","size":9},{"aid":"36658103","thumb_id":"163954451","owner_id":"6015870","title":"Моя ката","description":"","created":"1216419744","updated":"1273698620","size":8},{"aid":"23100962","thumb_id":"112723283","owner_id":"6015870","title":"Только пипл","description":"","created":"1208636545","updated":"1210382181","size":9},{"aid":"15473894","thumb_id":"114370266","owner_id":"6015870","title":"other","description":"","created":"1203516879","updated":"1327679223","size":29},{"aid":"15471241","thumb_id":"95266020","owner_id":"6015870","title":"Сам батя или чего нельзя в фотошопе =)","description":"","created":"1203516081","updated":"1203516728","size":4}]}
[4]   Done                    curl -s https://api.vk.com/method/photos.getAlbums?uid=6015870

コマンドが生成する引用符を使用すると、次のようになります。

$ curl -s "https://api.vk.com/method/photos.getAlbums?uid=6015870&access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37" | python -m json.tool
{
    "error": {
        "error_code": 5, 
        "error_msg": "User authorization failed: access_token was given to another ip address.", 
        "request_params": [
            {
                "key": "oauth", 
                "value": "1"
            }, 
            {
                "key": "method", 
                "value": "photos.getAlbums"
            }, 
            {
                "key": "uid", 
                "value": "6015870"
            }, 
            {
                "key": "access_token", 
                "value": "0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37"
            }
        ]
    }
}

私のIPアドレスに適切なアクセストークンがないことは明らかなので、おそらくあなたにとってはより有用な情報が得られるでしょう。:-)

于 2013-03-24T11:39:02.223 に答える
5

あなたのURLを引用してください。これは壊れています:

curl https://api.github.com/repos/mojombo/jekyll/issues?state=closed&assignee=mojombo | python -mjson.tool

これは正しいです:

curl 'https://api.github.com/repos/mojombo/jekyll/issues?state=closed&assignee=mojombo' | python -mjson.tool
于 2013-03-24T11:36:44.487 に答える