【问题标题】:Error while parsing json file with objects使用对象解析 json 文件时出错
【发布时间】: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


【解决方案1】:

json 数组的格式为

[ json 1 , json 2]

你正在做的是将 json1 连接到 json 2...

在解析前尝试添加“[”和“]”

看看这个array of json

所有可解释的 Json 都应该是有效的..你可以validate Online too 让你的数据看起来像这样有助于你解析

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

【讨论】:

  • 我刚把 utf8 从 utf-8 改了。但仍然出现同样的错误。
  • 是的。但是现在如何更改我的 json 格式?我有很多这种格式的对象
  • 做一些操作...当时或读取或写入文件...例如写入时添加逗号,然后添加...& 读取时添加 [ & ]
猜你喜欢
  • 2013-03-06
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
  • 2014-12-18
相关资源
最近更新 更多