我一直在努力解决这个问题。我环顾四周,没有人提出这个问题。#'#'在错误消息中使用。
据我所知,JSON.parse()接受一个字符串,然后将其转换为一个对象。
我已在文本fileLine中读取,然后将其拆分到有逗号的位置。然后我加入了必要的数据部分,然后在方法中传递了它。它给了我上面的错误。
示例数据:
0,Lemons,lemons@gmauil.cpon,sppil,{"_root":{"data":0,"children":[{"data":{"filename":"gmsnet2.jpg","timeCreated":"2018-02-07","size":159},"children":[]},{"data":{"filename":"IMG_20160203_221955.jpg","timeCreated":"2018-02-07","size":3229224},"children":[]},{"data":{"filename":"Lemons","timeCreated":"-","size":"-"},"children":[{"data":{"filename":"hello","timeCreated":"-","size":"-"},"children":[{"data":{"filename":"IMG_20160205_193530.jpg","timeCreated":"2018-02-07","size":4115235},"children":[]},{"data":{"filename":"gmsnet2.jpg","timeCreated":"2018-02-07","size":159},"children":[]}]}]},{"data":{"filename":"dir2","timeCreated":"-","size":"-"},"children":[{"data":{"filename":"IMG_20160203_221955.jpg","timeCreated":"2018-02-07","size":3229224},"children":[]}]}]}}
减少了传递内容的例子:
{"_root":{"data":0,"children":[{"data":{"filename":"gmsnet2.jpg","timeCreated":"2018-02-07","size":159},"children":[]},{"data":{"filename":"IMG_20160203_221955.jpg","timeCreated":"2018-02-07","size":3229224},"children":[]}]}}
正在使用的代码:
var json = String(fileLine.split(',').slice(4).join(','));
var rootdata = JSON.parse(json);
我已经使用JSONLint测试了JSON,它说它是有效的json。 它在第一次被调用时工作,但是第二次被调用时它会崩溃。
我调试了它并得到了:
%7B%22_root%22%3A%7B%22data%22%3A0%2C%22children%22%3A%5B%7B%22data%22%3A%7B%22filename%22%3A%22gmsnet2.jpg%22%2C%22timeCreated%22%3A%222018-02-07%22%2C%22size%22%3A159%7D%2C%22children%22%3A%5B%5D%7D%2C%7B%22data%22%3A%7B%22filename%22%3A%22IMG_20160203_221955.jpg%22%2C%22timeCreated%22%3A%222018-02-07%22%2C%22size%22%3A3229224%7D%2C%22children%22%3A%5B%5D%7D%2C%7B%22data%22%3A%7B%22filename%22%3A%22Lemons%22%2C%22timeCreated%22%3A%22-%22%2C%22size%22%3A%22-%22%7D%2C%22children%22%3A%5B%7B%22data%22%3A%7B%22filename%22%3A%22hello%22%2C%22timeCreated%22%3A%22-%22%2C%22size%22%3A%22-%22%7D%2C%22children%22%3A%5B%7B%22data%22%3A%7B%22filename%22%3A%22IMG_20160205_193530.jpg%22%2C%22timeCreated%22%3A%222018-02-07%22%2C%22size%22%3A4115235%7D%2C%22children%22%3A%5B%5D%7D%2C%7B%22data%22%3A%7B%22filename%22%3A%22gmsnet2.jpg%22%2C%22timeCreated%22%3A%222018-02-07%22%2C%22size%22%3A159%7D%2C%22children%22%3A%5B%5D%7D%5D%7D%5D%7D%2C%7B%22data%22%3A%7B%22filename%22%3A%22dir2%22%2C%22timeCreated%22%3A%22-%22%2C%22size%22%3A%22-%22%7D%2C%22children%22%3A%5B%7B%22data%22%3A%7B%22filename%22%3A%22IMG_20160203_221955.jpg%22%2C%22timeCreated%22%3A%222018-02-07%22%2C%22size%22%3A3229224%7D%2C%22children%22%3A%5B%5D%7D%5D%7D%5D%7D%7D
它不会第二次打印。 任何帮助将不胜感激。