json - 使用 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'
}
解决方案
通过在过滤器之间使用管道,您可以将输入上下文更改为先前过滤器生成的值。当您select/1
接听电话时,输入的是代号值。您需要先将上下文保留到设备,根据型号进行选择,然后获取代号。
$ ... | jq --arg model "$model" '.devices[] | select(.model == $model).codename'
推荐阅读
- node.js - 使用 Mongoose 库创建查询(在数组中查找对象)
- redis - 在 Redis 中建模数据。哪个更好:排序集和字符串或哈希表?
- intellij-idea - 如何从 IntelliJ IDEA 中的插件更改字体大小?
- vue.js - nuxtjs axios 跨域资源共享问题
- java - 为什么我收到 Lucene TopDocs totalHits 类型的错误
- java - 在内部服务中捕获 AccessDeniedException 时防止事务回滚
- javascript - React 路由器 - url 更改但视图没有更改
- r - 如何从年、月和日的整数值初始化 R 中的日期
- python - spacy 代替 nltk 用于 POS 标记
- python - ImportError:无法从“elasticsearch”导入名称“Elasticsearch”