Chinaunix
标题:
如何通过shell取得下面JSON里的数据
[打印本页]
作者:
icefishxmg
时间:
2017-04-14 08:26
标题:
如何通过shell取得下面JSON里的数据
JSON文件内容如下:
[
{
"target": "test",
"datapoints": [
[
150662802.886176,
1491871680
],
[
148571954.715462,
1491871740
],
[
151503913.757496,
1491871800
]
]
}
]
如何通过shell取得里面的值分别写到一个数组里?
就是所有后面带小数点的乘8取出来放到一个数组里,不带小数点的乘1000放到另外一个数组里
作者:
jason680
时间:
2017-04-14 09:24
回复
1#
icefishxmg
$
a
=(`awk 'BEGIN{split("8 1000",m)}/\[/{c=0}/^ *[0-9.]+/{$1*=m[++c];if(
c==1
)printf("
%f
\n",$1)}' a.json`)
$ echo ${a[@]}
1205302423.089408 1188575637.723696 1212031310.059968
$
b
=(`awk 'BEGIN{split("8 1000",m)}/\[/{c=0}/^ *[0-9.]+/{$1*=m[++c];if(
c==2
)printf("
%d
\n",$1)}' a.json`)
$ echo ${b[@]}
1491871680000 1491871740000 1491871800000
作者:
icefishxmg
时间:
2017-04-14 09:40
回复
2#
jason680
如果我的数组数量特别多,怎么能遍历所有的然后分别入组呢?
作者:
jason680
时间:
2017-04-14 09:44
回复
3#
icefishxmg
栗子?
what do you want? or example ...
作者:
icefishxmg
时间:
2017-04-14 09:53
回复
4#
jason680
知道怎么弄了,谢谢您了!
作者:
icefishxmg
时间:
2017-04-14 10:03
本帖最后由 icefishxmg 于 2017-04-14 10:10 编辑
回复
4#
jason680
兄弟,如果我的这个JSON数据有上千行,我想去最后300组数据怎么做?我的原始数组是平行数组,我得用JQ命令给整理格式,怎么写到您这个代码里面呢?整理格式的代码是 cat test.json | jq .
还有我整理了下您的代码,因为我每个数的中间要加逗号,所以您的代码我给改成这样:
a=(`awk 'BEGIN{split("8 1000",m)}/\[/{c=0}/^ *[0-9.]+/{$1*=m[++c];if(c==1)printf("%f,\n",$1)}' a.json`)
但是有个问题,生成的数组最后一个后面也有逗号,最后一个怎么能让他没有逗号呢?
作者:
jason680
时间:
2017-04-14 10:26
回复
6#
icefishxmg
please give the information as below:
1. input data
2. procedure/process
3. output data
作者:
本友会机友会摄友会
时间:
2017-04-14 12:55
提示:
作者被禁止或删除 内容自动屏蔽
作者:
icefishxmg
时间:
2017-04-14 13:50
回复
7#
jason680
数据:[{"target": "tx", "datapoints": [[109005337.0656, 1491954360], [101993738.536408, 1491954420], [102060412.252589, 1491954480], [101379713.736238, 1491954540]]}, {"target": "rx", "datapoints": [[378849290.100856, 1491959700], [383391367.51079, 1491959760], [381350186.271844, 1491959820], [374395372.257555, 1491959880]]}
最终整理成的样子是要从后往前比如最后3组数据
{
"constr": "Chart",
"options": {"xAxis":{"type":"datetime"},"series":[{"data":[[数组里后面的数*1000,前面的数*8],[数组里后面的数*1000,前面的数*8],"type":"line"},{"data":[数组里后面的数*1000,前面的数*8],[数组里后面的数*1000,前面的数*8],"type":"line"}]}
}
前面的data里是target为tx的,后面的data里是target为rx的
作者:
jason680
时间:
2017-04-14 18:38
本帖最后由 jason680 于 2017-04-14 18:49 编辑
回复
9#
icefishxmg
$ cat a.json
[{"target": "tx", "
datapoints
": [[109005337.0656, 1491954360], [101993738.536408, 1491954420],
[102060412.252589, 1491954480], [101379713.736238, 1491954540]
]}, {"target": "rx", "datapoints": [[378849290.100856, 1491959700], [383391367.51079, 1491959760],
[381350186.271844, 1491959820], [374395372.257555, 1491959880]
]}]
$ jq '.[] | {data: .
datapoints
}' a.json | awk -v
last=2
'BEGIN{printf("
{\n%4s\"constr\": \"Chart\",\n%4s\"options\": {\"xAxis\":{\"type\":\"datetime\"},\"series\":[
",x,x)}/"data":/{++d;b=c=1;sd=$1}/^ +[0-9.,]+$/{a[d,b%last,c]=$1;++c}/^ +[0-9.]+$/{++b;c=1}END{for(nd=1;nd<=d;++nd){s=s ndc "{" sd "[";ndc=", ";nbc="";for(nb=0;nb<last;++nb){k=(nb+b)%last;s=s nbc sprintf("[%d, %f]",a[nd,k,2]*1000,a[nd,k,1]*8);nbc=", "}s=s "], \"type\":\"line\"}"}print s "]}\n}"}'
{
"constr": "Chart",
"options": {"xAxis":{"type":"datetime"},"series":[
{"data":[[1491954480000, 816483298.020712], [1491954540000, 811037709.889904]], "type":"line"}, {"data":[[1491959820000, 3050801490.174752], [1491959880000, 2995162978.060440]], "type":"line"}]}
}
作者:
icefishxmg
时间:
2017-04-17 13:39
回复
10#
jason680
兄弟,我两组数组最后的type内容不一样应该怎么改呢?一个要spline一个要areaspline
作者:
jason680
时间:
2017-04-17 14:37
回复
11#
icefishxmg
Would you modify it by yourself
作者:
icefishxmg
时间:
2017-04-17 14:58
回复
12#
jason680
我自己琢磨了半天没实现,还请大神帮下忙,谢谢!十分感谢!
作者:
本友会机友会摄友会
时间:
2017-04-18 15:09
提示:
作者被禁止或删除 内容自动屏蔽
作者:
icefishxmg
时间:
2017-04-18 15:45
回复
15#
本友会机友会摄友会
你这个是用什么写的?
作者:
jason680
时间:
2017-04-19 13:17
本帖最后由 jason680 于 2017-04-24 11:00 编辑
回复
11#
icefishxmg
4/24 11:00 禁用表情(笑臉)
$ jq '.[] | {data: .datapoints}' a.json | awk -vlast=2 'BEGIN{printf("{\n%4s\"constr\": \"Chart\",\n%4s\"options\": {\"xAxis\":{\"type\":\"datetime\"},\"series\":[",x,x)}/"data":/{++d;b=c=1;sd=$1}/^ +[0-9.,]+$/{a[d,b%last,c]=$1;++c}/^ +[0-9.]+$/{++b;c=1}END{for(nd=1;nd<=d;++nd){s=s ndc "{" sd "[";ndc=", ";nbc="";for(nb=0;nb<last;++nb){k=(nb+b)%last;s=s nbc sprintf("[%d, %f]",a[nd,k,2]*1000,a[nd,k,1]*8);nbc=", "}s=s "], \"type\":\""((++cnt==1)?"":"area")"spline\"}"}print s "]}\n}"}'
{
"constr": "Chart",
"options": {"xAxis":{"type":"datetime"},"series":[{"data":[[1491954480000, 816483298.020712], [1491954540000, 811037709.889904]], "type":"spline"}, {"data":[[1491959820000, 3050801490.174752], [1491959880000, 2995162978.060440]], "type":"areaspline"}]}
}
作者:
icefishxmg
时间:
2017-04-24 08:09
本帖最后由 icefishxmg 于 2017-04-24 08:24 编辑
回复
17#
jason680
$ jq '.[] | {data: .datapoints}' a.json | awk -vlast=2 'BEGIN{printf("{\n%4s\"constr\": \"Chart\",\n%4s\"options\": {\"xAxis\":{\"type\":\"datetime\"},\"series\":[",x,x)}/"data":/{++d;b=c=1;sd=$1}/^ +[0-9.,]+$/{a[d,b%last,c]=$1;++c}/^ +[0-9.]+$/{++b;c=1}END{for(nd=1;nd<=d;++nd){s=s ndc "{" sd "[";ndc=", ";nbc="";for(nb=0;nb<last;++nb){k=(nb+b)%last;s=s nbc sprintf("[%d, %f]",a[nd,k,2]*1000,a[nd,k,1]*
8)
;nbc=", "}s=s "], \"type\":\""((++cnt==1)?"":"area"
;)
"spline\"}"}print s "]}\n}"}'
作者:
icefishxmg
时间:
2017-04-24 08:25
回复
17#
jason680
我把笑脸转换过来,执行还是报错
作者:
icefishxmg
时间:
2017-04-25 08:42
本帖最后由 icefishxmg 于 2017-04-25 09:59 编辑
回复
17#
jason680
终于搞定了!谢谢兄弟!十分感谢!
作者:
JcccZ
时间:
2019-01-16 20:42
回复
16#
jason680
您好,json文件内容如下
{
"targets" : [
"Yinhuahua",
"GoldPigLoan",
"RandomLoan",
"FortuneWallet"
]
}
我需要在另外一个shell脚本里读取这个json文件的数组并保存为变量,给后续脚本使用。
请问如何编写脚本?
非常抱歉,shell新手,还请不吝赐教
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2