首页 > 解决方案 > JSON 解析 -SyntaxError: 位置 72 处 JSON 中的意外标记

问题描述

我已经生成了几个单行 JSON 文件(来自 excel)——我试图通过 cordova 移动应用程序读取这些文件。该文件正在被读取,但问题是解析它示例在线 Json 文件:注意文件正在被读取

在onloadend里面:

{"name":"Acrobat Ant","id":"500","category":"Ants","features":{"format": {"cn":"hd_lst" ,"fm":"inline","edit": 0},"npma":"1.Ants//13.Wood-infesting Insects","compare2":"","length":"Adults  2.5-4mm // Queen  10mm","color":"Brown to black // "... (this is only partial 

Error in Success callbackId: File899903995 : SyntaxError: Unexpected token   in JSON at position 72
   cordova.js:310 SyntaxError: Unexpected token   in JSON at position 72
    at JSON.parse (<anonymous>)
    at FileReader.reader.onloadend (plugins.js:188)
    at FileReader.readSuccessCallback (plugins/cordova-plugin-file/www/FileReader.js:164)
    at Object.callbackFromNative (cordova.js:291)
    at <anonymous>:1:9

这发生在我尝试的单个文件中。该位置 72(对于此文件)位于
冒号之前的 {"format" 单词的末尾:正如我所说,除了位置更改之外的所有其他文件都会发生这种情况,但所有文件都在格式属性的末尾完全失败。

我使用https://jslint.com/检查单行 JSON 的语法,它说很好。奇怪的是,如果将一个衬里更改为分层格式(通过可视代码 Shift-ALt-F),因为下面显示的示例分解 JSON 可以毫无问题地解析。

{
        "name": "Acrobat Ant",
        "id": "500",
        "category": "Ants",
        "features": {
            "format": {
                "cn": "hd_lst",
                "fm": "inline",
                "edit": 0
            },
            ....etc

顺便说一句,一行 JSON 文件是从 Excel 生成的,并保存为 UTF8 without BOM。(如果我尝试使用 BOM 解析它会失败。)所以我的困境是我有超过 200 个文件,我希望它们在创建后立即读取,而无需执行手动额外步骤将它们变成分层格式 JSON。

仅供参考,我完全使用了有关如何从 VBA 将 excel 数据保存到没有 BOM 的 UTF8 的建议:save a file with UTF-8 without BOM

请指教

标签: jsonvbacordovautf-8

解决方案


推荐阅读