Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Linux bash shell和下划线解析JSON_Json_Linux_Bash_Underscore.js - Fatal编程技术网

使用Linux bash shell和下划线解析JSON

使用Linux bash shell和下划线解析JSON,json,linux,bash,underscore.js,Json,Linux,Bash,Underscore.js,下面的代码不能满足我的需要。我想将$STR传递给下划线,并从JSON数据中提取name属性 #!/bin/bash STR='['$@']'; RESULT=`underscore pluck --data '+$STR+' name`; echo $RESULT; JSON数据: {"maxResults":1,"resultList":[{"@class":"com.sohnar.trafficlite.transfer.crm.refactor.ClientCRMEntryTO","id

下面的代码不能满足我的需要。我想将$STR传递给下划线,并从JSON数据中提取name属性

#!/bin/bash
STR='['$@']';
RESULT=`underscore pluck --data '+$STR+' name`;
echo $RESULT;
JSON数据:

{"maxResults":1,"resultList":[{"@class":"com.sohnar.trafficlite.transfer.crm.refactor.ClientCRMEntryTO","id":331458,"version":2,"dateCreated":"2017-05-31T13:20:22.960+0000","dateModified":"2017-06-05T14:23:59.961+0000","lastUpdatedUserId":71954,"name":"ACME_CLIENT","website":null,"description":null,"billingLocation":null,"primaryLocation":null,"crmEntryType":"CLIENT","industryType":null,"accountManagerId":103049,"crmClientClassificationListItemId":{"id":12405},"companyProfile":{"id":486024,"version":1,"dateCreated":"2017-05-31T13:20:22.960+0000","dateModified":"2017-06-05T14:23:59.962+0000","sourceOfBusinessListItemId":null,"creditTermsListItemId":{"id":4215},"relationshipSince":"2017-05-30T23:00:00.000+0000","turnover":0,"employees":0,"taxNumber":null,"companyNumber":null,"nominalCode":null,"accountPackageId":null,"optOutMarketing":false,"optOutEmail":false,"optOutTelephone":false,"notes":null},"colorCode":0,"externalCode":"SAP-01","clientState":"CLIENT","defaultCustomRateSetId":null,"preferredCurrencyId":{"id":48},"freeTags":[]}],"windowSize":5,"currentPage":1}

这里有几个问题。首先,应该使用双引号,而不是单引号。双引号允许扩展变量。其次,我假设您不希望+作为数据的一部分-它们在bash中不是字符串串联运算符,因此不需要

str="[$@]"
result=$(underscore pluck --data "$str" name)
echo $result
我使用了$notation而不是back ticks```。回勾被认为是不推荐使用的,并且很难阅读

我用小写替换了大写变量名。这是因为shell使用了许多大写变量名,因此使用大写可能会导致名称冲突。最好使用小写或大小写混合的变量名

我还删除了多余的分号;。它们没有造成任何伤害,但也没有用。在bash中,分号是语句分隔符,而在C和Java等语言中,分号是语句终止符

编辑:双引号$@

我猜出来了

#!/bin/bash
name=$(underscore select .name --outfmt text < get.crm.client.acme.json)
echo $name

$STR未在单引号内展开。将“+$STR+”替换为+$STR+”执行此操作时,我会遇到以下错误:在“lax”模式下解析“-data”参数时出错:标记无效或意外。能否编辑帖子并包含运行的命令、实际结果和预期结果?将有效的JSON数据分散到多个参数上,以这样一种方式,将这些参数与它们之间的空格连接起来,可以生成一个可用的文档?编辑你的问题以显示一个真实的用例,这样我们就不需要对数据做太多的假设。通过将$str改为“$str”,在前后添加单引号,我得到了以下结果。现在的结果是:[null,null,null,null]要传递给下划线的数据的单引号部分是吗?您将向$@传递什么?你期望什么价值?下划线是什么?我之所以得到[null,null,null,null],是因为它将我的输入视为字符串文字。@Berni在你的问题中给出了可能包含在str中的JSON数据示例。我强烈建议改为str=[$*],这实际上是明确的,而str=[$@]意味着用户希望保留参数边界。那么,您的问题中的$@是怎么回事?如果您告诉我们下划线程序从stdin读取JSON,我们可以节省很多时间。