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

使用Brickset API反序列化JSON后pieces字段值显示0的问题

问题分析与解决方案

你的核心问题是JSON结构和C#类层级不匹配:返回的JSON根对象包含status、matches和sets数组,而你定义的Rootobject类对应的是sets数组里的单个套装对象,直接反序列化到Rootobject会导致所有属性无法正确映射,因此输出pieces为默认值0。

修正步骤:

  1. 创建对应JSON根结构的响应类
    先定义包含根层级属性的类,用来接收完整的API返回数据:

    public class BricksetResponse
    {
        public string status { get; set; }
        public int matches { get; set; }
        public List<Set> sets { get; set; }
    }
    
  2. 重命名原类为单个套装的实体类
    将你原来的Rootobject改名为Set,它对应sets数组里的单个套装数据:

    public class Set
    {
        public int setID { get; set; }
        public string number { get; set; }
        public int numberVariant { get; set; }
        public string name { get; set; }
        public int year { get; set; }
        public string theme { get; set; }
        public string themeGroup { get; set; }
        public string subtheme { get; set; }
        public string category { get; set; }
        public bool released { get; set; }
        public int pieces { get; set; }
        public int minifigs { get; set; }
        public Image image { get; set; }
        public string bricksetURL { get; set; }
        public Collection collection { get; set; }
        public Collections collections { get; set; }
        public Legocom LEGOCom { get; set; }
        public float rating { get; set; }
        public int reviewCount { get; set; }
        public string packagingType { get; set; }
        public string availability { get; set; }
        public int instructionsCount { get; set; }
        public int additionalImageCount { get; set; }
        public Agerange ageRange { get; set; }
        public Dimensions dimensions { get; set; }
        public Barcode barcode { get; set; }
        public Extendeddata extendedData { get; set; }
        public DateTime lastUpdated { get; set; }
    }
    

    注:确保你已定义Image、Collection、Collections等嵌套类(原代码中应该已有这些类的定义)。

  3. 修正反序列化逻辑
    在GetPosts方法中,先反序列化为根响应类,再从sets数组中取第一个结果(因为matches=1,说明只有一个匹配项):

    private async Task GetPosts(string url)
    {
        using HttpClient client = new HttpClient(); // 使用using自动释放资源
        string response = await client.GetStringAsync(url);
        Console.WriteLine(response);
        var bricksetResponse = JsonConvert.DeserializeObject<BricksetResponse>(response);
        if (bricksetResponse?.sets != null && bricksetResponse.sets.Count > 0)
        {
            var set = bricksetResponse.sets[0];
            Console.WriteLine(set.pieces); // 现在会输出正确的零件数,比如1445
            // 也可以正常读取其他属性,如set.name、set.year等
        }
        else
        {
            Console.WriteLine("未找到对应套装数据");
        }
    }
    
  4. 优化URL参数的JSON格式
    原代码中用单引号拼接JSON参数,不符合标准JSON规范,建议改为双引号(注意转义):

    string setNumber = "{\"setNumber\":\"" + Console.ReadLine().Trim() + "-1\"}";
    

完成以上修改后,就能正确读取到套装的pieces及其他属性值了。

内容的提问来源于stack exchange,提问作者Dennis.b

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 01:55:18