无法从动态json获取数据

时间:2018-11-13 17:47:20

标签: javascript json angular

这是我的json

mat-select
通过使用上述

我想存储在变量中,并希望将其用于其他目的。因此,在按钮上单击即时消息即可处理数据

[{
    "_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"

下面是我的stackblitzurl

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

所以在这里,我希望上面的数据需要输出,而我得到上面的输出

2 个答案:

答案 0 :(得分:1)

您的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);
  }

答案 1 :(得分:1)

尝试一下:

scala> case class IPC(name: String, `class`: String)
defined class IPC

scala> val x = Seq(IPC("a", "b"), IPC("d", "e")).toDF
java.lang.UnsupportedOperationException: `class` is a reserved keyword and cannot be used as field name
- root class: "IPC"
  at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$org$apache$spark$sql$catalyst$ScalaReflection$$serializerFor$1$$anonfun$8.apply(ScalaReflection.scala:627)
...

scala> case class IPC(name: String, clazz: String)
defined class IPC

scala> val x = Seq(IPC("a", "b"), IPC("d", "e")).toDF
x: org.apache.spark.sql.DataFrame = [name: string, clazz: string]

scala> x.select($"clazz".as("class")).show(false)
+-----+
|class|
+-----+
|b    |
|e    |
+-----+

scala> 

enter image description here