这是我输入的样子:
[
{
"Description": "Description 1"
"OutputKey": "OutputKey 1"
"OutputValue": "OutputValue 1"
}
{
"Description": "Description 2"
"OutputKey": "OutputKey 2"
"OutputValue": "OutputValue 2"
}
{
"Description": "Description 3"
"OutputKey": "OutputKey 3"
"OutputValue": "OutputValue 3"
}
{
"Description": "Description 4"
"OutputKey": "OutputKey 4"
"OutputValue": "OutputValue 4"
}
{
"Description": "Description 5"
"OutputKey": "OutputKey 5"
"OutputValue": "OutputValue "
}
{
"Description": "Description 6"
"OutputKey": "OutputKey 6"
"OutputValue": "OutputValue 6"
}
]
我需要遍历数组中的每个对象并获取OutputKey和OutPutValue并在另一个函数中使用它。
如何遍历此数组并获得所需的值?
我在Windows上使用GitBash。
答案 0 :(得分:1)
1)您的JSON无效,这是正确的版本(input.json
):
[
{
"Description": "Description 1",
"OutputKey": "OutputKey 1",
"OutputValue": "OutputValue 1"
},
{
"Description": "Description 2",
"OutputKey": "OutputKey 2",
"OutputValue": "OutputValue 2"
},
{
"Description": "Description 3",
"OutputKey": "OutputKey 3",
"OutputValue": "OutputValue 3"
},
{
"Description": "Description 4",
"OutputKey": "OutputKey 4",
"OutputValue": "OutputValue 4"
},
{
"Description": "Description 5",
"OutputKey": "OutputKey 5",
"OutputValue": "OutputValue "
},
{
"Description": "Description 6",
"OutputKey": "OutputKey 6",
"OutputValue": "OutputValue 6"
}
]
2)至于json表示 JavaScript Object Notation ,我在js脚本中使用nodejs和shell:
#!/bin/bash
node<<EOF
var arr = $(cat input.json);
arr.forEach(function(el){ console.log(el.OutputKey + " " + el.OutputValue ); })
EOF
OutputKey 1 OutputValue 1
OutputKey 2 OutputValue 2
OutputKey 3 OutputValue 3
OutputKey 4 OutputValue 4
OutputKey 5 OutputValue
OutputKey 6 OutputValue 6