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

Newtonsoft.Json反序列化后所有值均为null的问题求助

问题原因及解决办法

核心问题1:静态属性无法被反序列化

Newtonsoft.Json默认只处理实例属性,你的ItemSer类中所有属性都是static(静态)的,反序列化过程不会对静态属性赋值,这是导致所有值为null的直接原因。

核心问题2:JSON结构与目标类不匹配

你的JSON最外层是包含config数组的对象,数组里才是多个ItemSer格式的元素,但你直接把整个JSON反序列化为单个ItemSer对象,结构完全不对应,自然无法正确解析。

核心问题3:错误的属性访问方式

你反序列化得到了实例m,但代码里直接访问GlobalVariables.ItemSer.ARSID静态属性,就算实例属性有值,也不会被读取到。


修正步骤

1. 修改ItemSer类,将静态属性改为实例属性

[Serializable]
public class ItemSer
{
    public string EssentialID { get; set; }
    public string MotherID { get; set; }
    public string SimpleID { get; set; }
    public string PassiveID { get; set; }
    public string ARSID { get; set; }
}

2. 创建对应JSON结构的根类

因为JSON最外层是带config数组的对象,需要定义一个根类来匹配这个结构:

public class ConfigRoot
{
    public List<ItemSer> config { get; set; }
}

3. 修正反序列化代码

using (StreamReader r = new StreamReader(filepath))
{
    string jsonString = r.ReadToEnd();
    // 反序列化为根类对象
    ConfigRoot root = JsonConvert.DeserializeObject<ConfigRoot>(jsonString);
    
    // 读取第一个元素的ARSID示例
    if (root.config != null && root.config.Count > 0)
    {
        Console.WriteLine(root.config[0].ARSID);
        // 如果需要赋值给全局变量,可手动处理(不推荐依赖静态变量)
        // GlobalVariables.ItemSer firstItem = root.config[0];
        // GlobalVariables.ItemSer.EssentialID = firstItem.EssentialID;
    }
    
    GlobalVariables.ApplicationState.loadjson = true;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 08:45:45