[英]Change JSON output with javascript
我正在使用Node使用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] } ] }
如您所见,键显示为beign,但值仍然是一个对象。 我希望以上数据相同,只是我也希望显示该值。
您可以使用[] .map()创建带有结果的新数组。 这是一个例子
var newResult = resultaten.entries.map(function(entry) {
var result = {};
entry.data.forEach(function (datum) {
result[datum.key] = datum.value.value;
})
return result;
})
您的值在那里,我认为console.log
仅显示网[Object]
的详细值的[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数据。 请检查您是否正在寻找这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.