【发布时间】:2018-04-16 22:23:06
【问题描述】:
当我解析单个对象时,它不会出错。在将第二个对象附加到 JSON 文件后,它给出了错误。我的文件有什么问题。 这是我添加第二个对象后的 json 文件:
{
"_id":1,
"cid":3067,
"name":"Tech Mahindra",
"type":["Information Technology and Services"],
"empSize":99704,
"empSizeInfo":[
{
"on":1507141800,
"size":99704
}
],
"domain":"http://www.techmahindra.com/theme/DAVID.html",
"loc":{
"ct":"Maharashtra",
"cy":"in",
"pCode":"411004"
},
"followers":370803,
"ctdOn":1507141800,
"status":0,
"updtdOn":1507141800,
"cmptd":1}
我的第二个目标是:
{
"_id":2,
"cid":3178,
"name":"JLL",
"type":["Commercial Real Estate"],
"empSize":45147,
"empSizeInfo":[
{
"on":1500229800,
"size":45147
}
],
"domain":"http://www.jll.com",
"followers":472853,
"ctdOn":1500229800,
"status":0,
"loc":{
"ct":"Haryana",
"cy":"in",
"pCode":"122002"
},
"cmptd":1}
添加第二个文档 nodejs JSON.parse() 后出现错误。 我的文件包含多个对象。 这是我的 nodejs 代码:
const fs= require("fs")
//const jsonfile= require("jsonfile")
//const bulkindex= require("./bulkindex.js")
const test= function(){
fs.readFile("a.json",'utf-8',function(err,data){
if(err) throw err;
data1=JSON.parse(data) // getting error here
//bulkindex.bulkIndex("mathced","profiles",data1);
console.log(data1)
});
//console.log(jsonfile.readFileSync("a.json"))
}
test()
这是错误:
undefined:25
{
^
SyntaxError: Unexpected token {
at Object.parse (native)
at /home/sys2047/Desktop/EmailPrediction/Elastic Search/test.js:7:14
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:380:3)
如何解析这个 json 文件。 ?
【问题讨论】:
-
你必须附加一个逗号,
-
你能显示生成的json吗
-
那么它将变成[object,object]。但这不是我需要的。我需要解析这个 json 文件。无需编辑任何内容。我的 json 文件包含数千个对象。
-
你可能必须在一个数组中设置这两个对象。尝试在jsoneditoronline.org等网站中验证json
-
在第一个文档之后出现解析错误。在 jsoneditoronline.org
标签: javascript json node.js object