【问题标题】:unable to get the data from dynamic json无法从动态 json 中获取数据
【发布时间】:2018-11-13 17:47:20
【问题描述】:

下面是我的json

[{
    "_hits": 2.163,
    "_type": "data",
    "_id": "11138",
    "_source": {
      "urls": "http://localhost:9618/info?data_id=11138",
      "host": "max",
      "roll": "11138",
      "information": {
        "type": "image/jpeg",
        "data_id": "11138",
        "data_size": 186497,
        "creation_utctime": "1494831805258",
      },
      "subhost": "sample"
    },
    "_index": "max"
  }
];

通过使用上述内容,我想将其存储在一个变量中,并希望将其用于其他目的。所以在按钮上点击我正在处理数据

<button type="button"(click)="getData()" >get Data</button>

 getData(){
  this.rows = [];
   for (var res in this.info){
    var row = {};
    for (var key in this.info[res]['_source']){
 for (var k in this.info[res]['_source'][key]){
let temp = key + "." + k;
row[temp] = this.info[res]['_source'][key][k];
 }
 row['_id'] = this.info[res]['_id'];
    }
 this.rows.push(row);
 console.log(this.rows);

   }
}

要求的输出是:

host:"max"
information.creation_utctime: "1494831805258"
information.data_id: "11138"
information.data_size: 186497
information.type: "image/jpeg"
roll:"11138"
subhost:"sample"

urls:"http://localhost:9618/info?data_id=11138"
_id: "11138"

我得到的输出是:

host.0: "m"
host.1: "a"
host.2: "x"
information.creation_utctime: "1494831805258"
information.data_id: "11138"
information.data_size: 186497
information.type: "image/jpeg"
roll.0: "1"
roll.1: "1"
roll.2: "1"
roll.3: "3"
roll.4: "8"
subhost.0: "s"
subhost.1: "a"
subhost.2: "m"
subhost.3: "p"
subhost.4: "l"
subhost.5: "e"
urls.0: "h"
urls.1: "t"
urls.10: "a"
urls.11: "l"
urls.12: "h"
urls.13: "o"
urls.14: "s"
urls.15: "t"
urls.16: ":"
urls.17: "9"
urls.18: "6"
urls.19: "1"
urls.2: "t"
urls.20: "8"
urls.21: "/"
urls.22: "i"
urls.23: "n"
urls.24: "f"
urls.25: "o"
urls.26: "?"
urls.27: "d"
urls.28: "a"
urls.29: "t"
urls.3: "p"
urls.30: "a"
urls.31: "_"
urls.32: "i"
urls.33: "d"
urls.34: "="
urls.35: "1"
urls.36: "1"
urls.37: "1"
urls.38: "3"
urls.39: "8"
urls.4: ":"
urls.5: "/"
urls.6: "/"
urls.7: "l"
urls.8: "o"
urls.9: "c"
_id: "11138"

下面是我的stackblitzurl

https://stackblitz.com/edit/angular-d7mnpz

所以这里我想要上面的数据需要输出,我得到上面的输出

【问题讨论】:

  • 你不需要第二个 for 循环。
  • @lbu 好的,但我希望输出是为此目的所需的输出,重要的是要正确

标签: javascript json angular


【解决方案1】:

试试这个:

getData(){
      this.rows = [];

      const nodeToFlat = '_source';

      this.info.forEach( (info,index) => {
        let tmpRowStr = {};
        for( let x in info[nodeToFlat]){
          if(info[nodeToFlat][x].constructor === Object){
            for (let z in info[nodeToFlat][x] ) {
              tmpRowStr[`${x}.${z}`] = info[nodeToFlat][x][z];
            }

          } else {
            tmpRowStr[`${x}`] = info[nodeToFlat][x];
          }
        }

        this.rows.push(tmpRowStr);
      }
     )

     console.log('======>' ,this.rows);


    }

【讨论】:

  • 不,我只是在 this.info 中添加了另一个 JSON 以显示它适用于许多元素
  • 好的,让我检查一下并回复您&我们可以使用另一个模板文字代替模板文字吗?我的意思是 $x 和 $z
  • 类似 [x,z].join('.') 而不是 ${x}.${z} ?我认为模板文字更好
【解决方案2】:

您的 getData() 将“.k”放在属性名称中。
只有当它是一个对象时,你才需要这样做。
这是正确的代码:

    getData() {
    this.rows = [];
    for (var res in this.info) {
      var row = {};
      for (var key in this.info[res]['_source']) {
        if (typeof this.info[res]['_source'][key] === 'object') {
          for (var k in this.info[res]['_source'][key]) {
            let temp = key + "." + k;
            row[temp] = this.info[res]['_source'][key][k];
          }
        } else {
          row[key] = this.info[res]['_source'][key]
        }
        row['_id'] = this.info[res]['_id'];
      }
    }
    this.rows.push(row);
    console.log(this.rows);
  }

【讨论】:

  • 但是,如果想要所需的输出,这很重要,我的意思是我想要特定所需输出格式的数据
  • 我想要这样的主机输出:"max" information.creation_utctime: "1494831805258" information.data_id: "11138" information.data_size: 186497 information.type: "image/jpeg" roll:"11138 " subhost:"sample" urls:"localhost:9618/info?data_id=11138" _id: "11138"
  • 刚刚编辑了我的答案。试试看是不是你想要的。
  • 你的解决方案没问题,但它只适用于我传递单个 json 对象,如果我传递超过 1 个 json 对象,它不起作用
猜你喜欢
  • 1970-01-01
  • 2013-11-18
  • 2018-06-19
  • 1970-01-01
  • 2017-08-02
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多