<分区>
我需要从 json 字符串中打印键和值。我已经解析了一个简单的 json 字符串
{
"Name": "test1",
"CreateDate": "2016-08-30T10:52:52Z",
"Id": "testId1",
}
我的代码是这样的
q1=$(echo $x | grep -Po '"Name":.*?[^\\]",'| perl -pe 's/"Name": //; s/^"//; s/",$//');
q2=$(echo $x | grep -Po '"Id":.*?[^\\]",'| perl -pe 's/"Id": //; s/^"//; s/",$//');
echo $q1 "," $q2;
但是这段代码不适用于这样的json字符串
x='{ "TestNames":
[{
"Name": "test1",
"CreateDate": "2016-08-30T10:52:52Z",
"Id": "testId1"
},
{
"Name": "test2",
"CreateDate": "2016-08-30T10:52:13Z",
"Id": "testId2"
}]
}';
我需要这样打印
test1 , testId1
test2 , testId2
是否可以使用 grep 命令获取这样的数据?