首页 > 解决方案 > 在 Unity 日志引用的 JSON 配置文件中无法理解/发现错误

问题描述

我已经为游戏 Valheim 中的一个 mod 定制了这个 JSON 配置文件。模组使用 Unity 程序来加载模组,它会显示控制台日志,因此您可以在使用所述模组加载游戏时发现错误。我在 Unity 控制台中收到有关文件的持续错误,但无法理解错误或在文件 JSON 格式中找到任何错误。

错误如下

[Info   :   BepInEx] Loading [Epic Loot 0.6.4]
[Error  : Unity Log] Exception: Unrecognized token at index 14451
Stack trace:
fastJSON.JsonParser.ParseValue (System.Char* p) (at <375de602811e45c183084f18a311bd14>:0)
fastJSON.JsonParser.ParseArray (System.Char* p) (at <375de602811e45c183084f18a311bd14>:0)
fastJSON.JsonParser.ParseValue (System.Char* p) (at <375de602811e45c183084f18a311bd14>:0)
fastJSON.JsonParser.ParseObject (System.Char* p) (at <375de602811e45c183084f18a311bd14>:0)
fastJSON.JsonParser.ParseValue (System.Char* p) (at <375de602811e45c183084f18a311bd14>:0)
fastJSON.JsonParser.ParseArray (System.Char* p) (at <375de602811e45c183084f18a311bd14>:0)
fastJSON.JsonParser.ParseValue (System.Char* p) (at <375de602811e45c183084f18a311bd14>:0)
fastJSON.JsonParser.ParseObject (System.Char* p) (at <375de602811e45c183084f18a311bd14>:0)
fastJSON.JsonParser.ParseValue (System.Char* p) (at <375de602811e45c183084f18a311bd14>:0)
fastJSON.JsonParser.Decode (System.Type objtype) (at <375de602811e45c183084f18a311bd14>:0)
fastJSON.deserializer.ToObject (System.String json, System.Type type) (at <375de602811e45c183084f18a311bd14>:0)
fastJSON.deserializer.ToObject[T] (System.String json) (at <375de602811e45c183084f18a311bd14>:0)
fastJSON.JSON.ToObject[T] (System.String json) (at <375de602811e45c183084f18a311bd14>:0)
EpicLoot.EpicLoot.LoadJsonFile[T] (System.String filename) (at <626516c9d3e244fa8f8ddd6519f94bb9>:0)
EpicLoot.EpicLoot.InitializeConfig () (at <626516c9d3e244fa8f8ddd6519f94bb9>:0)
EpicLoot.EpicLoot.Awake () (at <626516c9d3e244fa8f8ddd6519f94bb9>:0)
UnityEngine.GameObject:AddComponent(Type)
BepInEx.Bootstrap.Chainloader:Start()
UnityEngine.Application:.cctor()"

这是文件

https://pastebin.com/RugHLkm0

预先感谢您的任何帮助。

标签: c#jsonunity3d

解决方案


错误消息的意思是第 14451 个字符是错误的(从 json 解析器的角度来看)。

这个字符索引对我不是很有帮助。因此,为了进一步探索这一点,我使用正则表达式删除了注释(在 regex101 链接中,单击左侧面板上的“替换”以查看没有注释的版本)。[*]

然后我将无注释版本粘贴到在线JSON 解析器中。

输出更精确一点:

Parse error on line 368:
..., "Weight": 1 }    },    {      "Obje
----------------------^
Expecting ',', ']', got '}'

使用notepad++等正确的文本编辑器找到第368行,这里确实有问题:

    {
      "Object": "Leech",
      "Drops": [ [0, 0], [1, 65], [2, 25], [3, 10] ],
      "Loot": [
        { "Item": "Tier2EnchantMats", "Weight": 1 }   <--- line 368 without the comments
    },

在我看来,你想要这个:

    {
      "Object": "Leech",
      "Drops": [ [0, 0], [1, 65], [2, 25], [3, 10] ],
      "Loot": [
        { "Item": "Tier2EnchantMats", "Weight": 1 }
      ]
    },

您忘记关闭数组 'Loot' !(需要右括号)

我会让你重复这个过程来检查其他语法错误。

注意:[*] 尽管一些 JSON 解析器可以容纳注释,但 JSON 规范根本没有授权它们,就像这个解析器一样。


推荐阅读