在 Linux Bash 中从 cURL 获取 JSON 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28971771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting JSON value from cURL in Linux Bash
提问by Kousha
I want to GET
some json data from a server. I do this using:
我想要GET
一些来自服务器的 json 数据。我这样做使用:
UPDATE=$(curl -i -H "Accept: application/json" -H "Content-Type: application/json" --cookie "${COOKIE_NAME}" "/update/${DEVICE_NAME}");
Before this, the server is authenticated. The ${1}
is the server domain, ${DEVICE_NAME}
is the name of the device requesting the update.
在此之前,服务器已通过身份验证。该${1}
是服务器领域,${DEVICE_NAME}
是请求更新的设备的名称。
This returns a JSON as follows:
这将返回一个 JSON,如下所示:
[{"_id":"54ff35887d8ef574029b9166","user":"54fe4313883bcec2c0ac0d64","__v":0,"created":"2015-03-10T18:18:48.023Z","status":"available","pbo_udid":"lemaker","installation_script":"","description":"Prints hello world to console","package_name":"helloworld_1.0-1.deb","name":"Hello World V1"}]
[{"_id":"54ff35887d8ef574029b9166","user":"54fe4313883bcec2c0ac0d64","__v":0,"created":"2015-03-10T18:18:48.023","stat","Z" pbo_udid":"lemaker","installation_script":"","description":"将hello world打印到控制台","package_name":"helloworld_1.0-1.deb","name":"Hello World V1"} ]
I want to now do 2 things:
我现在想做两件事:
- Make sure data is returned (if no update is available, the server returns
[]
- Extract data, for instance
package_name
- 确保返回数据(如果没有可用更新,服务器返回
[]
- 提取数据,例如
package_name
How do I do these in Linux bash script?
我如何在 Linux bash 脚本中执行这些操作?
采纳答案by Kousha
Assuming there's no nested array:
假设没有嵌套数组:
cat <<EOF | json_reformat | \
sed -rne '/:/s@^\s+"(\w+)":\s+"([^"]+)",?@json_=""@gp'
[{"_id":"54ff35887d8ef574029b9166","user":"54fe4313883bcec2c0ac0d64","__v":0,"created":"2015-03-10T18:18:48.023Z","status":"available","pbo_udid":"lemaker","installation_script":"","description":"Prints hello world to console","package_name":"helloworld_1.0-1.deb","name":"Hello World V1"}]
EOF
returns
回报
json__id="54ff35887d8ef574029b9166"
json_user="54fe4313883bcec2c0ac0d64"
json_created="2015-03-10T18:18:48.023Z"
json_status="available"
json_pbo_udid="lemaker"
json_description="Prints hello world to console"
json_package_name="helloworld_1.0-1.deb"
json_name="Hello World V1"
You need json_reformat
for this to work.
你需要json_reformat
这个才能工作。
EDIT: without json_reformat
:
编辑:没有json_reformat
:
cat <<EOF | \
sed -re 's@(\[|\]|\{|\})@@g' -e 's/,/\n/g' | \
sed -re 's@"(\w+)":\s*"?([^"]*)"?@json_=""@g'
[{"_id":"54ff35887d8ef574029b9166","user":"54fe4313883bcec2c0ac0d64","__v":0,"created":"2015-03-10T18:18:48.023Z","status":"available","pbo_udid":"lemaker","installation_script":"","description":"Prints hello world to console","package_name":"helloworld_1.0-1.deb","name":"Hello World V1"}]
EOF
It returns (note the version number that is reformatted anyway):
它返回(注意无论如何重新格式化的版本号):
json__id="54ff35887d8ef574029b9166"
json_user="54fe4313883bcec2c0ac0d64"
json___v="0"
json_created="2015-03-10T18:18:48.023Z"
json_status="available"
json_pbo_udid="lemaker"
json_installation_script=""
json_description="Prints hello world to console"
json_package_name="helloworld_1.0-1.deb"
json_name="Hello World V1"
You can now try parsing this text using eval
or source it from stdin.
您现在可以尝试使用eval
stdin 或从 stdin 中解析此文本。