可以在JS中使用requient方法读取我的JSON文件
我正在尝试循环浏览我从文件中删除的JSON数据。我使用的是需要函数来获取JSON文件,但是它一直告诉我有一个意外的“ {”令牌? IDE和控制台上的位置有些偏离,但我很确定这是开放{ - 位置44-
有任何建议的第二个对象吗?
mydata.json
{
"colour" : "blue",
"cm" : 2
}
{
"colour" : "red",
"cm" : 5
}
index.js
const fileContents = require("./myData.json");
I'm trying to loop through JSON data that I'm pulling from a file. I'm using the require function to get the JSON file but it keeps telling me there is an unexpected '{' token? positions on IDE and console are a little off but I'm pretty sure it's the second objects opening { -position 44-
Any suggestions?
myData.json
{
"colour" : "blue",
"cm" : 2
}
{
"colour" : "red",
"cm" : 5
}
index.js
const fileContents = require("./myData.json");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为 JSON 无效。您的 JSON 文件需要定义一个实体,而当前您正在定义两个实体。即使代码可以读取数据,也没有逻辑方法来指示您要引用这两个对象中的哪一个。
看起来您打算将它们放入一个数组中:
然后您可以导入整个数组,并引用每个对象的数组元素。
Because the JSON is invalid. Your JSON file needs to define one entity, where currently you're defining two. Even if the code could read the data, there's no logical way to indicate which of those two objects you want to refer to.
It looks like you meant to put them in an array:
Then you can import the whole array, and refer to elements of the array for each object.