最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何在Javascript中解析过滤JSON文件中的数据

IT培训 admin 5浏览 0评论

如何在Javascript中解析/过滤JSON文件中的数据

有没有办法解析/过滤Javascript文件中JSON文件中的数据。

基本上,我将JSON文件调用到我的本地javascript中。我在阅读特定数据和打印时遇到了麻烦。

谁能请帮忙。

JSON文件包含:

{
    "Data": [
    {

    "name": "John",
    "age": 30
    },

    {
    "joined on":"Jan 2015",
    "working on": "Automation",
    }
]
}

我试图将上述JSON文件读作:

var jfile = require("./Example.json");

var test = JSON.parse(JSON.stringify(jfile))

console.log(test)

我得到这样的输出:

{ Data:
   [ { name: 'John', age: 30 },
     { 'joined on': 'Jan 2015', 'working on': 'Automation' } ] }

从上面,我有兴​​趣访问/过滤掉一个即“名称”。我想只将值“John”打印到控制台。

我曾尝试对JSON.parse方法使用“.filter”方法,但它会抛出一个错误:

JSON.parse(...).filter is not a function

有没有办法执行此活动?

回答如下:

您可以使用.点表示法访问它

var a = {
  "Data": [{

      "name": "John",
      "age": 30
    },

    {
      "joined on": "Jan 2015",
      "working on": "Automation",
    }
  ]
}
console.log(a.Data[0].name)

如何在Javascript中解析/过滤JSON文件中的数据

有没有办法解析/过滤Javascript文件中JSON文件中的数据。

基本上,我将JSON文件调用到我的本地javascript中。我在阅读特定数据和打印时遇到了麻烦。

谁能请帮忙。

JSON文件包含:

{
    "Data": [
    {

    "name": "John",
    "age": 30
    },

    {
    "joined on":"Jan 2015",
    "working on": "Automation",
    }
]
}

我试图将上述JSON文件读作:

var jfile = require("./Example.json");

var test = JSON.parse(JSON.stringify(jfile))

console.log(test)

我得到这样的输出:

{ Data:
   [ { name: 'John', age: 30 },
     { 'joined on': 'Jan 2015', 'working on': 'Automation' } ] }

从上面,我有兴​​趣访问/过滤掉一个即“名称”。我想只将值“John”打印到控制台。

我曾尝试对JSON.parse方法使用“.filter”方法,但它会抛出一个错误:

JSON.parse(...).filter is not a function

有没有办法执行此活动?

回答如下:

您可以使用.点表示法访问它

var a = {
  "Data": [{

      "name": "John",
      "age": 30
    },

    {
      "joined on": "Jan 2015",
      "working on": "Automation",
    }
  ]
}
console.log(a.Data[0].name)
发布评论

评论列表 (0)

  1. 暂无评论