C# 反序列化包含Dictionary参数的对象会导致Dictionary成为空对象
这个json对象包含一个Person对象列表,每个Person对象上都有一个字典C# 反序列化包含Dictionary参数的对象会导致Dictionary成为空对象,c#,json,dictionary,json.net,C#,Json,Dictionary,Json.net,这个json对象包含一个Person对象列表,每个Person对象上都有一个字典 { "$type": "PeopleDataObject, Assembly-CSharp", "People": { "$type": "System.Collections.Generic.List`1[[Person, Assembly-CSharp]], msc
{
"$type": "PeopleDataObject, Assembly-CSharp",
"People": {
"$type": "System.Collections.Generic.List`1[[Person, Assembly-CSharp]],
mscorlib",
"$values": [
{
"$type": "Person, Assembly-CSharp",
"Id": 0,
"FirstName": "Daffy",
"LastName": "Duck",
"Age": 0,
"Sex": true,
"Stats": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String,
mscorlib],[System.Int32, mscorlib]], mscorlib",
"str": 9,
"dex": 13,
"con": 9,
"int": 13,
"wis": 10,
"cha": 8
}
},
{
"$type": "Person, Assembly-CSharp",
"Id": 1,
"FirstName": "Wilma",
"LastName": "Flintstone",
"Age": 0,
"Sex": false,
"Stats": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String,
mscorlib],[System.Int32, mscorlib]], mscorlib",
"str": 7,
"dex": 9,
"con": 9,
"int": 12,
"wis": 12,
"cha": 12
}
}
]
},
"Count": 2
}
通过将字符串写入另一个文件,我检查了读取的字符串是否格式不正确,因此我知道所有内容都正确序列化,但在反序列化时,字典丢失,因此当我再次使用或保存它时,它们为空。调试器将其标识为空对象。下面的代码遵循一般对象的文档,但我找不到包含字典的对象的特定情况。我确保包含TypeNameHandling,以确保它知道反序列化到字典中,但我没有得到那个结果
private void LoadRoster()
{
string path = Path.Combine(Application.streamingAssetsPath, "PeopleData.json");
string debug = Path.Combine(Application.streamingAssetsPath, "debug.json");
if (!File.Exists(path))
{
Debug.Log("NO SAVE FILE RECOGNISED");
}
else
{
PeopleDataObject allPeople = PeopleDataObject.Instance;
string jsonString = File.ReadAllText(path);
//check that the file is read correctly
File.WriteAllText(debug, jsonString);
allPeople = JsonConvert.DeserializeObject<PeopleDataObject>(jsonString, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
People = allPeople.People;
Person.AllTimeCount = allPeople.Count;
Debug.Log("Loaded");
}
}
private void加载花名册()
{
string path=path.Combine(Application.streamingassetpath,“PeopleData.json”);
string debug=Path.Combine(Application.streamingassetpath,“debug.json”);
如果(!File.Exists(path))
{
Debug.Log(“未识别任何保存文件”);
}
其他的
{
PeopleDataObject allPeople=PeopleDataObject.Instance;
字符串jsonString=File.ReadAllText(路径);
//检查文件是否已正确读取
writealText(debug,jsonString);
allPeople=JsonConvert.DeserializeObject(jsonString,新JsonSerializerSettings
{
TypeNameHandling=TypeNameHandling.All
});
人=所有人。人;
Person.AllTimeCount=allPeople.Count;
Debug.Log(“已加载”);
}
}
编辑:它试图反序列化到下面的PeopleDataObject类,该类本身包含一个我将在下面提供的人员列表
PeopleDataObject:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PeopleDataObject
{
//<Singleton Boilerplate>
private static PeopleDataObject instance = null;
private static readonly object padlock = new object();
PeopleDataObject() {}
public static PeopleDataObject Instance
{
get
{
lock(padlock)
{
if (instance == null)
{
instance = new PeopleDataObject();
}
return instance;
}
}
}
//</Singletone Boilerplate>
public List<Person> People { get; set; }
public int Count { get; set; }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类PeopleDataObject
{
//
私有静态PeopleDataObject实例=null;
私有静态只读对象挂锁=新对象();
PeopleDataObject(){}
公共静态PeopleDataObject实例
{
得到
{
锁(挂锁)
{
if(实例==null)
{
实例=新的PeopleDataObject();
}
返回实例;
}
}
}
//
公共列表人员{get;set;}
公共整数计数{get;set;}
}
人:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Person
{
public int Id { get; private set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; private set; }
public bool Sex { get; private set; } //true = male, false = female
public Dictionary<string, int> Stats { get; private set; }
public static int AllTimeCount { get; set; }
private GameObject messageController;
public Person(string first, string last, bool sex, GameObject controller)
{
Debug.Log(" overload 1");
FirstName = first;
LastName = last;
Age = 0;
Sex = sex;
Id = AllTimeCount;
AllTimeCount++;
if (controller != null)
messageController = controller;
else
messageController = GameObject.Find("PersonManager");
//if sex is true (male) colour modifier is blue, else colour modifier is pink
string colourMod = sex ? "#4286f4" : "#ff56ff";
Notification(string.Format("<{0}>{1} {2}</color> was born. ", colourMod, first, last));
}
public Person(int id, string first, string last, bool sex, Dictionary<string, int> stats)
{
Debug.Log("overload 2");
Id = id;
FirstName = first;
LastName = last;
Sex = sex;
Stats = stats;
}
public Person()
{
Debug.Log("overload 3");
}
public void SetStats(Dictionary<string, int> s)
{
Stats = s;
}
public void IncrementAge()
{
Age++;
}
public void Notification(string message)
{
MessageController controller = messageController.GetComponent<MessageController>();
controller.PostMessage(message);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共阶层人士
{
public int Id{get;private set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共整数{get;私有集;}
公共布尔性别{get;private set;}//true=男性,false=女性
公共字典统计数据{get;private set;}
公共静态int-AllTimeCount{get;set;}
私有游戏对象控制器;
公众人物(字符串优先、字符串最后、布尔性别、游戏对象控制器)
{
Log(“重载1”);
FirstName=first;
LastName=last;
年龄=0;
性别=性别;
Id=所有时间计数;
AllTimeCount++;
如果(控制器!=null)
messageController=控制器;
其他的
messageController=GameObject.Find(“PersonManager”);
//若性别为真(男性),则颜色修改器为蓝色,否则颜色修改器为粉色
字符串colorMod=sex?”#4286f4:“#ff56ff”;
通知(string.Format(“{1}{2}诞生了。”,colormod,first,last));
}
公共人物(int-id、字符串第一、字符串最后、bool-sex、字典统计)
{
Log(“重载2”);
Id=Id;
FirstName=first;
LastName=last;
性别=性别;
统计数据=统计数据;
}
公众人士()
{
Log(“重载3”);
}
公共无效设置状态(字典s)
{
Stats=s;
}
公共无效增量()
{
年龄++;
}
公共无效通知(字符串消息)
{
MessageController=MessageController.GetComponent();
controller.PostMessage(消息);
}
}
顺便说一句,我知道当使用构造函数2或3时,messageController-in-Person是空的,但这不是我现在关心的问题,它与当前的问题没有关系,但会被看到
编辑:已尝试过打字处理。自动而不是打字处理。全部无效
这件事我已经纠结了一段时间了。我已经知道我是一个白痴,但我非常感谢你的帮助,告诉我为什么我是一个白痴
干杯,
Dan答案是我的财产的可访问性 我在年龄、性别和统计数据上使用了
{get;private set;}
,因为我认为最佳实践是控制可访问性,但我应该使用{get;set}
,以便json库能够在反序列化时将值写入对象
这对于字典来说更为明显,因为年龄int默认为零,而且我所有的测试对象都是0岁的婴儿;性默认为假(女性),我只是没有发现;dictionary默认为null,您会丢失大量数据,因此这是我注意到的症状。如果没有控制序列化的PeopleDataObject的定义,我们将无能为力。显示类的代码。我们需要查看其中的一部分来帮助您。请参阅和,以了解如何编写我们可能能够回答的问题的一些技巧。啊,对不起。我想我可能发布了太多的代码,看起来我没有尝试。编辑以包括PeopleDataObject和Person。