{"record":{"id":"b34b425ebf4b688d","repo":"babalae/better-genshin-impact","slug":"json-ex-message","errorCode":null,"errorMessage":"JSON 战斗策略格式错误：{ex.Message}","messagePattern":"JSON 战斗策略格式错误：(.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/GameTask/AutoFight/Script/JsonCombatStrategyParser.cs","lineNumber":51,"sourceCode":"    }\n\n    /// <summary>\n    /// 从 JSON 字符串解析战斗策略\n    /// </summary>\n    /// <param name=\"json\">JSON 字符串</param>\n    /// <returns>解析后的战斗策略</returns>\n    /// <exception cref=\"InvalidOperationException\">解析失败或格式错误</exception>\n    public static JsonCombatStrategy Parse(string json)\n    {\n        JsonCombatStrategy? strategy;\n        try\n        {\n            strategy = JsonConvert.DeserializeObject<JsonCombatStrategy>(json);\n        }\n        catch (JsonException ex)\n        {\n            Logger.LogError(\"JSON 战斗策略解析失败：{Msg}\", ex.Message);\n            throw new InvalidOperationException($\"JSON 战斗策略格式错误：{ex.Message}\", ex);\n        }\n\n        if (strategy == null)\n        {\n            Logger.LogError(\"JSON 战斗策略反序列化结果为空\");\n            throw new InvalidOperationException(\"JSON 战斗策略反序列化失败\");\n        }\n\n        if (strategy.Info == null)\n        {\n            Logger.LogError(\"JSON 战斗策略缺少 Info 节点\");\n            throw new InvalidOperationException(\"JSON 战斗策略缺少 Info 节点\");\n        }\n\n        if (strategy.Actions == null || strategy.Actions.Count == 0)\n        {\n            Logger.LogError(\"JSON 战斗策略缺少 Actions 节点或动作为空\");\n            throw new InvalidOperationException(\"JSON 战斗策略中未定义任何动作\");","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/GameTask/AutoFight/Script/JsonCombatStrategyParser.cs#L33-L69","documentation":"Thrown by JsonCombatStrategyParser.Parse when Newtonsoft.Json JsonConvert.DeserializeObject throws a JsonException. This means the JSON string is syntactically invalid — malformed JSON structure, unquoted strings, trailing commas (depending on settings), or encoding issues. The original JsonException message is included in the InvalidOperationException message.","triggerScenarios":"The JSON strategy file content fails Newtonsoft.Json deserialization: unbalanced braces, missing quotes around keys/values, trailing commas in strict mode, BOM or encoding corruption, or accidentally passing non-JSON content (e.g. a .txt combat script) to Parse().","commonSituations":"User hand-edits a .json strategy file and introduces a syntax error (missing comma, unclosed brace, unquoted value). Also occurs when the file encoding is wrong (e.g., UTF-16 passed as UTF-8) or when the file is actually a TXT script mistakenly fed to the JSON parser.","solutions":["Validate the JSON file with a JSON linter or editor with JSON syntax checking.","Check the ex.Message in the exception for the exact JSON parse error location (line/position).","Ensure the file is valid UTF-8 without BOM corruption.","Confirm the file is actually a JSON strategy, not a TXT combat script."],"exampleFix":"// before (invalid JSON — missing quotes)\n{ Info: { Name: \"test\" } }\n\n// after (valid JSON)\n{ \"Info\": { \"Name\": \"test\" } }","handlingStrategy":"try-catch","validationCode":"// Validate JSON syntax before passing to the parser\nusing System.Text.Json;\n\nstatic bool IsValidJson(string json)\n{\n    try\n    {\n        System.Text.Json.JsonDocument.Parse(json);\n        return true;\n    }\n    catch\n    {\n        return false;\n    }\n}\n\nif (!IsValidJson(jsonContent))\n    throw new ArgumentException(\"策略文件不是有效的 JSON\");","typeGuard":null,"tryCatchPattern":"try\n{\n    var strategy = JsonCombatStrategyParser.Parse(json);\n}\ncatch (InvalidOperationException e) when (e.Message.Contains(\"格式错误\"))\n{\n    Logger.LogError(\"JSON 策略格式错误: {Msg}\", e.InnerException?.Message ?? e.Message);\n    // Show the user the JSON parse error location from the inner JsonException\n}","preventionTips":["Use a JSON-aware editor (VS Code, Rider) with syntax validation when editing strategy files.","Validate JSON with a linter before loading.","The inner JsonException message includes line/position — use it to locate the syntax error.","Never pass a .txt combat script to the JSON parser."],"tags":["json-strategy","deserialization","json","newtonsoft"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}