You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在C#中将JSON格式关卡数据转换为对应类对象的技术求助

JSON转C#关卡数据实体类失败解决方案

问题背景

我正在开发一款可编辑JSON格式关卡数据的C#程序,尝试将JSON文件转换为对应的C#实体类,但折腾了两小时始终失败,请求解决办法。

提供的JSON示例

{
    "ambienceSettings": {
        "fogDDensity": 0.57235307,
        "skyHorizonColor": {
            "a": 1.0,
            "b": 0.010186112,
            "r": 0.12088394
        },
        "skyZenithColor": {
            "a": 1.0
        },
        "sunAltitude": 45.0,
        "sunAzimuth": 315.0
    },
    "complexity": 127,
    "formatVersion": 6,
    "levelNodes": [
        {
            "isLocked": true,
            "levelNodeStatic": {
                "material": "LAVA",
                "position": {
                    "x": 709.82574,
                    "y": 2778.5,
                    "z": 613.0453
                },
                "rotation": {
                    "w": 0.8660254,
                    "y": 0.5
                },
                "scale": {
                    "x": 3084.0,
                    "y": 108.0,
                    "z": 4136.5
                },
                "shape": "CUBE"
            }
        },
        {
            "levelNodeStatic": {
                "color": {
                    "a": 1.0,
                    "r": 0.5
                },
                "material": "DEFAULT_COLORED",
                "position": {
                    "x": 464.0625,
                    "y": 2832.7517,
                    "z": 509.81253
                },
                "rotation": {
                    "w": -4.371139e-08,
                    "y": -1.0
                },
                "scale": {
                    "x": 2.874939,
                    "y": 0.4958496,
                    "z": 2.875
                },
                "shape": "CUBE"
            }
        },
        {
            "levelNodeStart": {
                "position": {
                    "x": 464.125,
                    "y": 2833.0,
                    "z": 510.8751
                },
                "radius": 0.22492802,
                "rotation": {
                    "w": 1.0,
                    "y": -2.0861619e-07
                }
            }
        }
    ]
}

注:原JSON存在语法错误,已修复未闭合的对象、数组结构

已定义的C#实体类(修正版)

using Newtonsoft.Json;
using System.Collections.Generic;

public class AmbienceSettings
{
    [JsonProperty("fogDDensity")]
    public double FogDDensity { get; set; }

    [JsonProperty("skyHorizonColor")]
    public SkyHorizonColor SkyHorizonColor { get; set; }

    [JsonProperty("skyZenithColor")]
    public SkyZenithColor SkyZenithColor { get; set; }

    [JsonProperty("sunAltitude")]
    public double SunAltitude { get; set; }

    [JsonProperty("sunAzimuth")]
    public double SunAzimuth { get; set; }
}

public class Color
{
    [JsonProperty("a")]
    public double A { get; set; }

    [JsonProperty("r")]
    public double R { get; set; }
}

public class LevelNode
{
    [JsonProperty("isLocked")]
    public bool? IsLocked { get; set; } // 改为可空类型,适配部分节点无该字段的情况

    [JsonProperty("levelNodeStatic")]
    public LevelNodeStatic LevelNodeStatic { get; set; }

    [JsonProperty("levelNodeStart")]
    public LevelNodeStart LevelNodeStart { get; set; }
}

public class LevelNodeStart
{
    [JsonProperty("position")]
    public Position Position { get; set; }

    [JsonProperty("radius")]
    public double Radius { get; set; }

    [JsonProperty("rotation")]
    public Rotation Rotation { get; set; }
}

public class LevelNodeStatic
{
    [JsonProperty("material")]
    public string Material { get; set; }

    [JsonProperty("position")]
    public Position Position { get; set; }

    [JsonProperty("rotation")]
    public Rotation Rotation { get; set; }

    [JsonProperty("scale")]
    public Scale Scale { get; set; }

    [JsonProperty("shape")]
    public string Shape { get; set; }

    [JsonProperty("color")]
    public Color Color { get; set; }
}

public class Position
{
    [JsonProperty("x")]
    public double X { get; set; }

    [JsonProperty("y")]
    public double Y { get; set; }

    [JsonProperty("z")]
    public double Z { get; set; }
}

public class Level
{
    [JsonProperty("ambienceSettings")]
    public AmbienceSettings AmbienceSettings { get; set; }

    [JsonProperty("complexity")]
    public int Complexity { get; set; }

    [JsonProperty("formatVersion")]
    public int FormatVersion { get; set; }

    [JsonProperty("levelNodes")]
    public List<LevelNode> LevelNodes { get; set; }

    [JsonProperty("maxCheckpointCount", NullValueHandling = NullValueHandling.Ignore)]
    public int? MaxCheckpointCount { get; set; } // 改为可空类型并忽略空值,适配JSON中无该字段的情况

    [JsonProperty("title", NullValueHandling = NullValueHandling.Ignore)]
    public string Title { get; set; } // 添加忽略空值属性,适配JSON中无该字段的情况
}

public class Rotation
{
    [JsonProperty("w")]
    public double W { get; set; }

    [JsonProperty("y")]
    public double Y { get; set; }

    [JsonProperty("x")]
    public double? X { get; set; }

    [JsonProperty("z")]
    public double? Z { get; set; }
}

public class Scale
{
    [JsonProperty("x")]
    public double X { get; set; }

    [JsonProperty("y")]
    public double Y { get; set; }

    [JsonProperty("z")]
    public double Z { get; set; }
}

public class SkyHorizonColor
{
    [JsonProperty("a")]
    public double A { get; set; }

    [JsonProperty("b")]
    public double B { get; set; }

    [JsonProperty("r")]
    public double R { get; set; }
}

public class SkyZenithColor
{
    [JsonProperty("a")]
    public double A { get; set; }
}

注:已对实体类做关键修改,适配JSON结构

解决方案步骤

1. 修复JSON语法错误

原JSON存在未闭合的对象和数组,这是反序列化失败的核心原因之一,必须先修正才能正常解析,修复后的JSON如上方示例所示。

2. 调整实体类适配JSON结构

  • LevelNode.IsLocked改为可空类型:部分节点没有isLocked字段,非可空布尔类型会导致反序列化时抛出缺失字段的异常。
  • Level类字段调整:MaxCheckpointCount改为可空int?,并为MaxCheckpointCount和Title添加NullValueHandling.Ignore属性,因为原JSON中没有这两个字段,忽略空值可以避免反序列化报错。

3. 执行反序列化代码

确保项目已安装Newtonsoft.Json(Json.NET)NuGet包,然后使用以下代码完成反序列化:

using Newtonsoft.Json;
using System.IO;

// 读取JSON文件内容
string jsonPath = "你的关卡JSON文件路径";
string jsonContent = File.ReadAllText(jsonPath);

// 反序列化到Level对象
try
{
    Level levelData = JsonConvert.DeserializeObject<Level>(jsonContent);
    // 反序列化成功,可在此处理levelData
}
catch (JsonException ex)
{
    // 捕获并输出错误信息,方便排查问题
    System.Console.WriteLine($"反序列化失败:{ex.Message}");
}

4. 排查问题的额外技巧

如果仍然失败,建议:

  • 捕获JsonException查看具体错误信息,明确是字段不匹配、类型转换错误还是其他语法问题。
  • 使用本地JSON编辑器验证JSON结构,确保没有隐藏的语法错误。
  • 逐行对比JSON字段和实体类的JsonProperty属性名称,确保完全一致。

内容的提问来源于stack exchange,提问作者goteki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 00:50:21