首页 > 解决方案 > 使用 jq 工具从模型中获取 android 代号

问题描述

尝试从https://github.com/jaredrummler/AndroidDeviceNames获取具有其型号和品牌的特定 android 设备的代号:

$ brand=ACER
$ model=B3-A20B
$ \curl -Ls https://github.com/jaredrummler/AndroidDeviceNames/raw/master/json/manufacturers/$brand.json | \
jq ".devices[].codename | select( .devices[].model == \"$model\" )" 
jq: error (at <stdin>:1114): Cannot index string with string "devices"
$ echo $?
5

编辑 1:对于那些可能感兴趣的人,我写了这个小 bash 函数,.bash_functions感谢@jeff-mercado:

getCodeName () {
    if [ $# != 2 ]; then
        echo "=> Usage: $FUNCNAME brand model" >&2
        return 1
    fi

    local brand=$1
    local model=$2
    local codeNameJSONDataBaseURL=https://github.com/jaredrummler/AndroidDeviceNames/raw/master/json/manufacturers
    local curl="$(which curl) -sL"

    $curl $codeNameJSONDataBaseURL/$brand.json | jq -r --arg model $model '.devices[] | select( .model | match($model;"i") ).codename'
}

标签: jsonjq

解决方案


通过在过滤器之间使用管道,您可以将输入上下文更改为先前过滤器生成的值。当您select/1接听电话时,输入的是代号值。您需要先将上下文保留到设备,根据型号进行选择,然后获取代号。

$ ... | jq --arg model "$model" '.devices[] | select(.model == $model).codename'

推荐阅读