在Linux Bash中从cURL获取JSON值 [英] Getting JSON value from cURL in Linux Bash

查看:733
本文介绍了在Linux Bash中从cURL获取JSON值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从服务器 GET 一些json数据。我这样使用:

I want to GET some json data from a server. I do this using:

UPDATE=$(curl -i -H "Accept: application/json" -H "Content-Type: application/json" --cookie "${COOKIE_NAME}" "${1}/update/${DEVICE_NAME}");

在此之前,服务器已通过身份验证。 $ {1} 是服务器域, $ {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.

这会传回JSON,如下所示:

This returns a JSON as follows:


[{_ 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.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"}]

我现在要做2件事:


  1. 确保返回数据(如果没有更新可用,服务器返回 []

  2. 提取数据,例如 package_name

  1. Make sure data is returned (if no update is available, the server returns []
  2. Extract data, for instance package_name

如何在Linux bash script?

How do I do these in Linux bash script?

推荐答案

假设没有嵌套数组:

cat <<EOF | json_reformat | \
    sed -rne '/:/s@^\s+"(\w+)":\s+"([^"]+)",?@json_\1="\2"@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

返回

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"


$ b b

您需要 json_reformat 才能使用。

EDIT :without json_reformat

EDIT : without json_reformat:

cat <<EOF | \
    sed -re 's@(\[|\]|\{|\})@@g' -e 's/,/\n/g' | \
    sed -re 's@"(\w+)":\s*"?([^"]*)"?@json_\1="\2"@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"

现在可以尝试使用 eval 解析此文本或从stdin源代码。

You can now try parsing this text using eval or source it from stdin.

这篇关于在Linux Bash中从cURL获取JSON值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆