我正在使用 Node.js 来使用 API。使用节点我可以获得数据列表。 当我使用 Stringify 时,我得到这样的东西:
{ totalNumEntries: 700,
entries:
[ { data:
[ { key: 'KEYWORD_TEXT',
value:
{ attributes: { 'xsi:type': 'StringAttribute' },
'Attribute.Type': 'StringAttribute',
value: 'red herring 9e23f4ad' } },
{ key: 'SEARCH_VOLUME',
value:
{ attributes: { 'xsi:type': 'LongAttribute' },
'Attribute.Type': 'LongAttribute',
value: '4574730' } } ] },
{ data:
[ { key: 'KEYWORD_TEXT',
value:
{ attributes: { 'xsi:type': 'StringAttribute' },
'Attribute.Type': 'StringAttribute',
value: 'nike 656e95f0' } },
{ key: 'SEARCH_VOLUME',
value:
{ attributes: { 'xsi:type': 'LongAttribute' },
'Attribute.Type': 'LongAttribute',
value: '3442386' } } ] },
{ data:
[ { key: 'KEYWORD_TEXT',
value:
{ attributes: { 'xsi:type': 'StringAttribute' },
'Attribute.Type': 'StringAttribute',
value: 'red herring 2bb32682' } },
{ key: 'SEARCH_VOLUME',
value:
{ attributes: { 'xsi:type': 'LongAttribute' },
'Attribute.Type': 'LongAttribute',
value: '2641524' } } ] },
{ data:
[ { key: 'KEYWORD_TEXT',
value:
{ attributes: { 'xsi:type': 'StringAttribute' },
'Attribute.Type': 'StringAttribute',
value: 'nike d4b589f6' } },
{ key: 'SEARCH_VOLUME',
value:
{ attributes: { 'xsi:type': 'LongAttribute' },
'Attribute.Type': 'LongAttribute',
value: '4778937' } } ] },
我现在的问题是。我想要数据的键值对,例如:
[
{
"KEYWORD_TEXT": "red herring 9e23f4ad",
"SEARCH_VOLUME": 4574730
},
{
"KEYWORD_TEXT": "nike 656e95f0",
"SEARCH_VOLUME": 3442386
},
etc...
]
在我的代码中我尝试了下一个:
targetingIdeaService.get({selector: selector}, function (error, result) {
var resultaten = result;
console.log(resultaten.entries[0]);
})
这个输出接近我想要的,但不完全是。这是我得到的:
{ data:
[ { key: 'KEYWORD_TEXT', value: [Object] },
{ key: 'COMPETITION', value: [Object] },
{ key: 'SEARCH_VOLUME', value: [Object] } ] }
正如您所看到的,键已显示,但值仍然是一个对象。我希望上述数据相同,但我也希望显示该值。
最佳答案
var ls = {
"totalNumEntries": 700,
"entries": [{
"data": [{
"key": "KEYWORD_TEXT",
"value": {
"attributes": {
"xsi:type": "StringAttribute"
},
"Attribute.Type": "StringAttribute",
"value": "red herring 9e23f4ad"
}
}, {
"key": "SEARCH_VOLUME",
"value": {
"attributes": {
"xsi:type": "LongAttribute"
},
"Attribute.Type": "LongAttribute",
"value": "4574730"
}
}]
}, {
"data": [{
"key": "KEYWORD_TEXT",
"value": {
"attributes": {
"xsi:type": "StringAttribute"
},
"Attribute.Type": "StringAttribute",
"value": "nike 656e95f0"
}
}, {
"key": "SEARCH_VOLUME",
"value": {
"attributes": {
"xsi:type": "LongAttribute"
},
"Attribute.Type": "LongAttribute",
"value": "3442386"
}
}]
}]
};
// stringified
var newList = [];
for (var i = 0; i < ls.entries.length; i++) {
var obj = ls.entries[i];
var newObj = {};
for (var j = 0; j < obj.data.length; j++) {
var contnt = obj.data[j];
newObj[contnt.key] = contnt.value.value;
}
newList.push(newObj);
}
console.log(newList)
我从你的问题中获取了一部分json数据。请检查您是否正在寻找此内容。
关于javascript - 使用 javascript 更改 JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45232762/