babalae/better-genshin-impact · error · InvalidOperationException

JSON 战斗策略格式错误:{ex.Message}

Error message

JSON 战斗策略格式错误:{ex.Message}

What it means

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.

Source

Thrown at BetterGenshinImpact/GameTask/AutoFight/Script/JsonCombatStrategyParser.cs:51

    }

    /// <summary>
    /// 从 JSON 字符串解析战斗策略
    /// </summary>
    /// <param name="json">JSON 字符串</param>
    /// <returns>解析后的战斗策略</returns>
    /// <exception cref="InvalidOperationException">解析失败或格式错误</exception>
    public static JsonCombatStrategy Parse(string json)
    {
        JsonCombatStrategy? strategy;
        try
        {
            strategy = JsonConvert.DeserializeObject<JsonCombatStrategy>(json);
        }
        catch (JsonException ex)
        {
            Logger.LogError("JSON 战斗策略解析失败:{Msg}", ex.Message);
            throw new InvalidOperationException($"JSON 战斗策略格式错误:{ex.Message}", ex);
        }

        if (strategy == null)
        {
            Logger.LogError("JSON 战斗策略反序列化结果为空");
            throw new InvalidOperationException("JSON 战斗策略反序列化失败");
        }

        if (strategy.Info == null)
        {
            Logger.LogError("JSON 战斗策略缺少 Info 节点");
            throw new InvalidOperationException("JSON 战斗策略缺少 Info 节点");
        }

        if (strategy.Actions == null || strategy.Actions.Count == 0)
        {
            Logger.LogError("JSON 战斗策略缺少 Actions 节点或动作为空");
            throw new InvalidOperationException("JSON 战斗策略中未定义任何动作");

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Validate the JSON file with a JSON linter or editor with JSON syntax checking.
  2. Check the ex.Message in the exception for the exact JSON parse error location (line/position).
  3. Ensure the file is valid UTF-8 without BOM corruption.
  4. Confirm the file is actually a JSON strategy, not a TXT combat script.

Example fix

// before (invalid JSON — missing quotes)
{ Info: { Name: "test" } }

// after (valid JSON)
{ "Info": { "Name": "test" } }
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate JSON syntax before passing to the parser
using System.Text.Json;

static bool IsValidJson(string json)
{
    try
    {
        System.Text.Json.JsonDocument.Parse(json);
        return true;
    }
    catch
    {
        return false;
    }
}

if (!IsValidJson(jsonContent))
    throw new ArgumentException("策略文件不是有效的 JSON");

Try / catch

try
{
    var strategy = JsonCombatStrategyParser.Parse(json);
}
catch (InvalidOperationException e) when (e.Message.Contains("格式错误"))
{
    Logger.LogError("JSON 策略格式错误: {Msg}", e.InnerException?.Message ?? e.Message);
    // Show the user the JSON parse error location from the inner JsonException
}

Prevention

When it happens

Trigger: 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().

Common situations: 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.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/b34b425ebf4b688d. Report an issue: GitHub.