【问题标题】:How to fetch value from json object in angular 2如何从角度2中的json对象中获取值
【发布时间】:2018-10-11 22:32:57
【问题描述】:

如何从 Angular 2 中的 json 对象中获取值?我的对象如下:

这里我想获取 type 的值。

我尝试了以下方法:

  1. console.log("RECORD TYPE IS:"+records.type);

  2. console.log**(records['type'])**;

但它正在打印 undefined

【问题讨论】:

  • 您能否粘贴来自 chrome 开发工具的响应而不是预览选项卡的响应。也可以试试console.log(typeof records)

标签: json angular


【解决方案1】:

你的记录对象有类型,所以你应该试试

console.log(record['type']);

【讨论】:

    【解决方案2】:

    当您将 json 写入记录时,考虑到 Records 是您的类/接口名称,而 type 是字段名称。 示例:

    var record=yourObjectName as Records
    console.log(record.type)
    

    【讨论】:

      【解决方案3】:

      似乎您有一个record 的包装对象,在 JSON 中将表示如下:

      var Object = {
       record: {
        id: 30,
        recordno: "2",
        type: "dynamic"
       }
      }
      

      因此读取这个对象的正确方法是:

      console.log(Object.record.type);
      

      完整代码示例:

      var Object = {
           record: {
            id: 30,
            recordno: "2",
            type: "dynamic"
           }
          };
      document.getElementById("test").innerHTML = "Object type is: " + Object.record.type;
      <span id="test"></span>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-02-06
        • 2019-07-15
        • 2021-02-14
        • 2019-11-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多