javascript - 使用 javascript 更改 JSON 输出

标签 javascript json

我正在使用 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/

相关文章:

javascript - 正在调用 Razor 功能的 MVC 4 按钮 onClick

javascript - 将一个模板注入(inject)另一个模板

javascript - If else 切换大小写不同的输出

android studio Logcat在单行和部分数据中显示json数据

android - 如何使用 JSON 对象在 Android 中显示来自 MySQL 数据库的图像

javascript - 向客户端发送 POST 响应以通过 html 页面显示

javascript - 日期选择器和自动完成 jquery 之间的冲突

java - 在 Eclipse 中使用 JSON

php - 使用PDO将json文件导入MySQL时如何处理标签?

json - 如何从json中获取未转义的值