JSON.parse导致JSON输入意外结束,但我的JSON是正确的

2024-09-20 03:45:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个创建json的python脚本,还有一个读取json的nodejs脚本:

  • python脚本
with open("music.json", "w") as write_file:
    json.dump(music_found, write_file, indent=4)

找到的音乐是一个对象数组

  • nodejs
import fs from 'fs'
import { cwd } from 'process';


let rawdata = fs.readFileSync(`${cwd()}/music.json`);
let music = JSON.parse(rawdata)
console.log(music);

我收到消息json输入意外结束

  • json示例
[
    {
        "user": "some_user1",
        "file": "@@enlbq\\_Music\\Infected Mushroom\\Return to the Sauce [2017] [HMCD94]\\09 - Infected Mushroom - Liquid Smoke.flac",
        "size": 42084572,
        "slots": true,
        "speed": 1003176
    },
    {
        "user": "some_user2",
        "file": "@@xfjpb\\Musiikkia\\Infected Mushroom\\Return to the Sauce\\09 Infected Mushroom - Liquid Smoke.flac",
        "size": 24617421,
        "slots": true,
        "speed": 541950
    },
    {
        "user": "some_user3",
        "file": "@@rxjpv\\MyMusic\\Infected Mushroom\\Infected Mushroom - Return To The Sauce (2017) [CD FLAC]\\09 - Liquid Smoke.flac",
        "size": 41769608,
        "slots": true,
        "speed": 451671
    }
]

我的json格式很好不是吗?我从4小时起就在这上面了,我一直在这上面。。。非常烦人^^希望有人能帮忙


Tags: 脚本jsonsizereturnmusicsomefssmoke
2条回答

你能确定路径是正确的吗? 我可以在js中正常读取您的数据,所以我认为问题在于路径

readFileSync

If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.

您需要设置编码或使用缓冲区的.toString(方法

相关问题 更多 >