Mad*_*man 9 python macos terminal json curl
我试图用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$
Run Code Online (Sandbox Code Playgroud)
为什么我会收到"无法解码JSON对象"?我请求的Url总是返回有效的json.如果我像$ echo jsonreponse |那样手动传递json python -m json.tool,它确实美化了json.
难道我做错了什么 ?
Mar*_*ers 14
您需要在网址周围加上引号:
curl -s "https://api.vk.com/method/photos.getAlbums?uid=6015870&access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37" | python -m json.tool
Run Code Online (Sandbox Code Playgroud)
该&字符是一个shell元字符,它将命令放在后台.因此,您没有将完整的URL传递给服务器,而是将命令curl -s https://api.vk.com/method/photos.getAlbums?uid=6015870放在后台.
这就是为什么贝壳回应[4] 8822你; 你把目前为止的第四份工作放入了工作队列,它被赋予了PID 8822.
剩下的命令是:
access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37 | python -m json.tool
Run Code Online (Sandbox Code Playgroud)
确实不会产生有效的JSON:
$ access_token=0275127e915981fe795840165e532169482cbdfc4ca1dbd48619a687a65fead88c468cdafe9743e231c37 | python -m json.tool
No JSON object could be decoded
Run Code Online (Sandbox Code Playgroud)
您可以在下一行看到完成后台的工作:
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
Run Code Online (Sandbox Code Playgroud)
使用命令生成:
$ 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"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
据推测,它会产生更多有用的信息,因为我没有正确的访问令牌来获取我的IP地址.:-)
引用你的网址.这个坏了:
curl https://api.github.com/repos/mojombo/jekyll/issues?state=closed&assignee=mojombo | python -mjson.tool
Run Code Online (Sandbox Code Playgroud)
这个是正确的:
curl 'https://api.github.com/repos/mojombo/jekyll/issues?state=closed&assignee=mojombo' | python -mjson.tool
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6798 次 |
| 最近记录: |