我试图解析从curl请求返回的JSON,就像这样:
curl 'http://twitter.com/users/username.json' |
sed -e 's/[{}]/''/g' |
awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}'
上面将JSON划分为多个字段,例如:
% ...
"geo_enabled":false
"friends_count":245
"profile_text_color":"000000"
"status":"in_reply_to_screen_name":null
"source":"web"
"truncated":false
"text":"My status"
"favorited":false
% ...
我如何打印一个特定的字段(由-v k=文本表示)?
使用Python使用Bash
在.bashrc文件中创建一个Bash函数:
function getJsonVal () {
python -c "import json,sys;sys.stdout.write(json.dumps(json.load(sys.stdin)$1))";
}
Then
curl 'http://twitter.com/users/username.json' | getJsonVal "['text']"
输出:
My status
下面是相同的函数,但是带有错误检查。
function getJsonVal() {
if [ \( $# -ne 1 \) -o \( -t 0 \) ]; then
cat <<EOF
Usage: getJsonVal 'key' < /tmp/
-- or --
cat /tmp/input | getJsonVal 'key'
EOF
return;
fi;
python -c "import json,sys;sys.stdout.write(json.dumps(json.load(sys.stdin)$1))";
}
其中$# -ne 1确保至少有一个输入,而-t 0确保从管道重定向。
这个实现的好处是,您可以访问嵌套的JSON值并返回JSON内容!=)
例子:
echo '{"foo": {"bar": "baz", "a": [1,2,3]}}' | getJsonVal "['foo']['a'][1]"
输出:
2
如果你想要更漂亮,你可以把数据打印出来:
function getJsonVal () {
python -c "import json,sys;sys.stdout.write(json.dumps(json.load(sys.stdin)$1, sort_keys=True, indent=4))";
}
echo '{"foo": {"bar": "baz", "a": [1,2,3]}}' | getJsonVal "['foo']"
{
"a": [
1,
2,
3
],
"bar": "baz"
}
使用PHP CLI解析JSON
这可以说是跑题了,但是由于优先级的原因,如果不提到我们值得信赖和忠诚的PHP,这个问题仍然是不完整的,对吗?
它使用相同的示例JSON,但让我们将其分配给一个变量以减少模糊性。
export JSON='{"hostname":"test","domainname":"example.com"}'
对于PHP来说,它使用了file_get_contents和PHP://stdin流包装器。
echo $JSON | php -r 'echo json_decode(file_get_contents("php://stdin"))->hostname;'
或者使用fgets和已经在CLI常量STDIN中打开的流。
echo $JSON | php -r 'echo json_decode(fgets(STDIN))->hostname;'
使用Python使用Bash
在.bashrc文件中创建一个Bash函数:
function getJsonVal () {
python -c "import json,sys;sys.stdout.write(json.dumps(json.load(sys.stdin)$1))";
}
Then
curl 'http://twitter.com/users/username.json' | getJsonVal "['text']"
输出:
My status
下面是相同的函数,但是带有错误检查。
function getJsonVal() {
if [ \( $# -ne 1 \) -o \( -t 0 \) ]; then
cat <<EOF
Usage: getJsonVal 'key' < /tmp/
-- or --
cat /tmp/input | getJsonVal 'key'
EOF
return;
fi;
python -c "import json,sys;sys.stdout.write(json.dumps(json.load(sys.stdin)$1))";
}
其中$# -ne 1确保至少有一个输入,而-t 0确保从管道重定向。
这个实现的好处是,您可以访问嵌套的JSON值并返回JSON内容!=)
例子:
echo '{"foo": {"bar": "baz", "a": [1,2,3]}}' | getJsonVal "['foo']['a'][1]"
输出:
2
如果你想要更漂亮,你可以把数据打印出来:
function getJsonVal () {
python -c "import json,sys;sys.stdout.write(json.dumps(json.load(sys.stdin)$1, sort_keys=True, indent=4))";
}
echo '{"foo": {"bar": "baz", "a": [1,2,3]}}' | getJsonVal "['foo']"
{
"a": [
1,
2,
3
],
"bar": "baz"
}
使用Python的JSON支持,而不是使用AWK!
就像这样:
curl -s http://twitter.com/users/username.json | \
python -c "import json,sys;obj=json.load(sys.stdin);print(obj['name']);"
macOS v12.3 (Monterey)删除了/usr/bin/python,因此对于macOS v12.3及更高版本,我们必须使用/usr/bin/python3。
curl -s http://twitter.com/users/username.json | \
python3 -c "import json,sys;obj=json.load(sys.stdin);print(obj['name']);"