gpt4 book ai didi

c# - JSON.NET 忽略了我所有没有 JsonProperty 属性的属性

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:58 27 4
gpt4 key购买 nike

我有一个 GameBoard 类,它有两种保存和加载方法:

    public void Save(string pFilePath)
{
var s = JsonConvert.SerializeObject(this);
System.IO.File.WriteAllText(pFilePath, s);
}

public static GameBoard Load(string pFilePath)
{
var s = System.IO.File.ReadAllText(pFilePath);
return JsonConvert.DeserializeObject<GameBoard>(s);
}

这些是我的 GameBoard 类的属性:

    #region Fields

public int Width { get; protected set; }

public int Height { get; protected set; }

public GameBoardCell[,] Cells { get; protected set; }

public bool WallSurrounded { get; set; }

#endregion

Save 方法效果很好,内容如下:

{"Width":3,"Height":3,"Cells":[[{"Content":0,"Ball":{"ColorID":0,"ID":"4481e52d-76fc-4335-805b-c47e36e3d57d"},"Target":null},{"Content":1,"Ball":null,"Target":null},{"Content":0,"Ball":null,"Target":null}],[{"Content":0,"Ball":null,"Target":null},{"Content":0,"Ball":null,"Target":null},{"Content":0,"Ball":null,"Target":null}],[{"Content":0,"Ball":null,"Target":null},{"Content":1,"Ball":null,"Target":null},{"Content":0,"Ball":null,"Target":{"ColorID":0,"ID":"aaf6f4ae-0a1a-4c3d-9608-f13ae9c4bba7"}}]],"WallSurrounded":true}

但是,当我对同一个文件使用 Load 方法时,我的所有属性都是 null 或默认值(0 表示整数)。

我必须将 [JsonProperty] 添加到我的每个属性才能正常工作。我以前使用过 Json.NET,我记得我没有添加任何属性。这是 Json.NET 的意图改变(我刚从 Nuget 为这个新项目获得 6.0.6),还是我做错了什么?

最佳答案

JSON.NET 忽略 privateprotectedinternal 属性 setter 。将属性转换为 public,或者用属性装饰它们。

https://json.codeplex.com/discussions/222774

关于c# - JSON.NET 忽略了我所有没有 JsonProperty 属性的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26955471/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com