babalae/better-genshin-impact · error · InvalidOperationException
JSON 战斗策略反序列化失败
Error message
JSON 战斗策略反序列化失败
What it means
Thrown by JsonCombatStrategyParser.Parse when JsonConvert.DeserializeObject returns null. This happens when the JSON content is literally the token 'null', or when the JSON structure deserializes to a null reference for the JsonCombatStrategy type (e.g., an empty object that maps to all-default-null properties).
Source
Thrown at BetterGenshinImpact/GameTask/AutoFight/Script/JsonCombatStrategyParser.cs:57
/// <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 战斗策略中未定义任何动作");
}
// 校验动作合法性(名称需能作为条件标识符解析;index 允许重复)
ValidateActions(strategy.Actions);
Logger.LogInformation("JSON 战斗策略加载完成:{Name},共 {Count} 个动作",View on GitHub (pinned to a7cb36712d)
Solutions
- Check that the JSON file contains a valid JSON object with 'Info' and 'Actions' properties.
- Ensure the file is not empty or containing just 'null'.
- Verify the root element is an object ({...}), not an array or primitive.
- Re-download or recreate the strategy file from a known-good template.
Example fix
// before (file content is just null)
null
// after
{
"Info": { "Name": "MyStrategy" },
"Actions": [ { "Name": "start", "Index": 1 } ]
} Defensive patterns
Strategy: validation
Validate before calling
// Check for null-content edge cases before parsing
var trimmed = json?.Trim();
if (string.IsNullOrEmpty(trimmed) || trimmed == "null")
{
throw new ArgumentException("策略文件内容为空或为 null");
}
var strategy = JsonCombatStrategyParser.Parse(json); Try / catch
try
{
var strategy = JsonCombatStrategyParser.Parse(json);
}
catch (InvalidOperationException e) when (e.Message.Contains("反序列化失败"))
{
Logger.LogError("策略反序列化结果为空,请检查文件内容");
} Prevention
- Ensure the JSON root is an object (starts with '{'), not 'null', an array, or empty.
- Check the file was fully written and not truncated or cleared.
- If downloading strategy files, verify the download completed successfully.
When it happens
Trigger: The JSON file contains just `null` as its root value, or the content is structurally empty/whitespace in a way that Newtonsoft maps to null. This is distinct from a JsonException (syntactically valid but semantically null).
Common situations: User accidentally saves a file containing 'null', an empty response from a download, or a file that was cleared but not properly rewritten. Could also occur if the JSON root is an array or primitive instead of an object (type mismatch causing null).
Related errors
- JSON 战斗策略格式错误:{ex.Message}
- JSON 战斗策略缺少 Info 节点
- JSON 战斗策略中未定义任何动作
- JSON 战斗策略中动作名称无法作为条件标识符解析:{action.Name}
- milliseconds 不能小于 0
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/c664fab4844f26c9.
Report an issue: GitHub.