由于某种原因,反序列化后 Json 中的非空字符串数组成为具有 null 值的类属性,而它应该变为 non-empty List<string>
。
这是json:
{"data":{"places":{"А":["037","038","039","040","041","042","043","044","045","046","049","050","051","052"]},"schemeId":"П01","scheme":"{\"scheme_type\":\"П01\",\"model\":{\"floor\":{\"1\":{\"width\":20,\"height\":4}}},\"places\":{\"floor\":{\"1\":[{\"y\":1,\"x\":2,\"w\":1,\"h\":1,\"num\":\"2\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":3,\"w\":1,\"h\":1,\"num\":\"4\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":4,\"w\":1,\"h\":1,\"num\":\"6\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":5,\"w\":1,\"h\":1,\"num\":\"8\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":6,\"w\":1,\"h\":1,\"num\":\"10\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":7,\"w\":1,\"h\":1,\"num\":\"12\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":8,\"w\":1,\"h\":1,\"num\":\"14\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":9,\"w\":1,\"h\":1,\"num\":\"16\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":10,\"w\":1,\"h\":1,\"num\":\"18\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":11,\"w\":1,\"h\":1,\"num\":\"20\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":12,\"w\":1,\"h\":1,\"num\":\"22\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":13,\"w\":1,\"h\":1,\"num\":\"24\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":14,\"w\":1,\"h\":1,\"num\":\"26\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":15,\"w\":1,\"h\":1,\"num\":\"28\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":16,\"w\":1,\"h\":1,\"num\":\"30\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":17,\"w\":1,\"h\":1,\"num\":\"32\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":18,\"w\":1,\"h\":1,\"num\":\"34\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":19,\"w\":1,\"h\":1,\"num\":\"36\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":2,\"x\":2,\"w\":1,\"h\":1,\"num\":\"1\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":3,\"w\":1,\"h\":1,\"num\":\"3\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":4,\"w\":1,\"h\":1,\"num\":\"5\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":5,\"w\":1,\"h\":1,\"num\":\"7\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":6,\"w\":1,\"h\":1,\"num\":\"9\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":7,\"w\":1,\"h\":1,\"num\":\"11\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":8,\"w\":1,\"h\":1,\"num\":\"13\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":9,\"w\":1,\"h\":1,\"num\":\"15\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":10,\"w\":1,\"h\":1,\"num\":\"17\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":11,\"w\":1,\"h\":1,\"num\":\"19\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":12,\"w\":1,\"h\":1,\"num\":\"21\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":13,\"w\":1,\"h\":1,\"num\":\"23\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":14,\"w\":1,\"h\":1,\"num\":\"25\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":15,\"w\":1,\"h\":1,\"num\":\"27\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":16,\"w\":1,\"h\":1,\"num\":\"29\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":17,\"w\":1,\"h\":1,\"num\":\"31\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":18,\"w\":1,\"h\":1,\"num\":\"33\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":19,\"w\":1,\"h\":1,\"num\":\"35\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":2,\"w\":1,\"h\":1,\"num\":\"54\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":3,\"w\":1,\"h\":1,\"num\":\"53\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":4,\"w\":1,\"h\":1,\"num\":\"52\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":5,\"w\":1,\"h\":1,\"num\":\"51\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":6,\"w\":1,\"h\":1,\"num\":\"50\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":7,\"w\":1,\"h\":1,\"num\":\"49\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":8,\"w\":1,\"h\":1,\"num\":\"48\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":9,\"w\":1,\"h\":1,\"num\":\"47\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":10,\"w\":1,\"h\":1,\"num\":\"46\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":11,\"w\":1,\"h\":1,\"num\":\"45\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":12,\"w\":1,\"h\":1,\"num\":\"44\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":13,\"w\":1,\"h\":1,\"num\":\"43\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":14,\"w\":1,\"h\":1,\"num\":\"42\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":15,\"w\":1,\"h\":1,\"num\":\"41\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":16,\"w\":1,\"h\":1,\"num\":\"40\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":17,\"w\":1,\"h\":1,\"num\":\"39\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":18,\"w\":1,\"h\":1,\"num\":\"38\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":19,\"w\":1,\"h\":1,\"num\":\"37\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":1,\"x\":1,\"h\":1,\"type\":\"wall\"},{\"y\":1,\"x\":2,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":4,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":6,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":8,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":10,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":12,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":14,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":16,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":18,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":20,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":21,\"h\":2,\"type\":\"wall\"},{\"y\":4,\"x\":2,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":1,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":4,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":6,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":8,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":10,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":12,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":14,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":16,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":18,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":20,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":21,\"h\":1,\"type\":\"wall\"},{\"y\":1,\"x\":1,\"w\":1,\"h\":1,\"type\":\"toilet\"},{\"y\":1,\"x\":20,\"w\":1,\"h\":1,\"type\":\"toilet\"}]}}}"}}
相同的 json,但已解析(为了更好的可读性):
{
"data": {
"places": {
"А": [
"037",
"038",
"039",
"040",
"041",
"042",
"043",
"044",
"045",
"046",
"049",
"050",
"051",
"052"
]
},
"schemeId": "П01",
"scheme": "{\"scheme_type\":\"П01\",\"model\":{\"floor\":{\"1\":{\"width\":20,\"height\":4}}},\"places\":{\"floor\":{\"1\":[{\"y\":1,\"x\":2,\"w\":1,\"h\":1,\"num\":\"2\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":3,\"w\":1,\"h\":1,\"num\":\"4\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":4,\"w\":1,\"h\":1,\"num\":\"6\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":5,\"w\":1,\"h\":1,\"num\":\"8\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":6,\"w\":1,\"h\":1,\"num\":\"10\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":7,\"w\":1,\"h\":1,\"num\":\"12\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":8,\"w\":1,\"h\":1,\"num\":\"14\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":9,\"w\":1,\"h\":1,\"num\":\"16\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":10,\"w\":1,\"h\":1,\"num\":\"18\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":11,\"w\":1,\"h\":1,\"num\":\"20\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":12,\"w\":1,\"h\":1,\"num\":\"22\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":13,\"w\":1,\"h\":1,\"num\":\"24\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":14,\"w\":1,\"h\":1,\"num\":\"26\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":15,\"w\":1,\"h\":1,\"num\":\"28\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":16,\"w\":1,\"h\":1,\"num\":\"30\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":17,\"w\":1,\"h\":1,\"num\":\"32\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":18,\"w\":1,\"h\":1,\"num\":\"34\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":1,\"x\":19,\"w\":1,\"h\":1,\"num\":\"36\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":2,\"x\":2,\"w\":1,\"h\":1,\"num\":\"1\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":3,\"w\":1,\"h\":1,\"num\":\"3\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":4,\"w\":1,\"h\":1,\"num\":\"5\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":5,\"w\":1,\"h\":1,\"num\":\"7\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":6,\"w\":1,\"h\":1,\"num\":\"9\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":7,\"w\":1,\"h\":1,\"num\":\"11\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":8,\"w\":1,\"h\":1,\"num\":\"13\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":9,\"w\":1,\"h\":1,\"num\":\"15\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":10,\"w\":1,\"h\":1,\"num\":\"17\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":11,\"w\":1,\"h\":1,\"num\":\"19\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":12,\"w\":1,\"h\":1,\"num\":\"21\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":13,\"w\":1,\"h\":1,\"num\":\"23\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":14,\"w\":1,\"h\":1,\"num\":\"25\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":15,\"w\":1,\"h\":1,\"num\":\"27\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":16,\"w\":1,\"h\":1,\"num\":\"29\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":17,\"w\":1,\"h\":1,\"num\":\"31\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":18,\"w\":1,\"h\":1,\"num\":\"33\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":2,\"x\":19,\"w\":1,\"h\":1,\"num\":\"35\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":2,\"w\":1,\"h\":1,\"num\":\"54\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":3,\"w\":1,\"h\":1,\"num\":\"53\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":4,\"w\":1,\"h\":1,\"num\":\"52\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":5,\"w\":1,\"h\":1,\"num\":\"51\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":6,\"w\":1,\"h\":1,\"num\":\"50\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":7,\"w\":1,\"h\":1,\"num\":\"49\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":8,\"w\":1,\"h\":1,\"num\":\"48\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":9,\"w\":1,\"h\":1,\"num\":\"47\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":10,\"w\":1,\"h\":1,\"num\":\"46\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":11,\"w\":1,\"h\":1,\"num\":\"45\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":12,\"w\":1,\"h\":1,\"num\":\"44\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":13,\"w\":1,\"h\":1,\"num\":\"43\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":14,\"w\":1,\"h\":1,\"num\":\"42\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":15,\"w\":1,\"h\":1,\"num\":\"41\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":16,\"w\":1,\"h\":1,\"num\":\"40\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":17,\"w\":1,\"h\":1,\"num\":\"39\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":4,\"x\":18,\"w\":1,\"h\":1,\"num\":\"38\",\"pos\":\"up\",\"type\":\"place\"},{\"y\":4,\"x\":19,\"w\":1,\"h\":1,\"num\":\"37\",\"pos\":\"down\",\"type\":\"place\"},{\"y\":1,\"x\":1,\"h\":1,\"type\":\"wall\"},{\"y\":1,\"x\":2,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":4,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":6,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":8,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":10,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":12,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":14,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":16,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":18,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":20,\"h\":2,\"type\":\"wall\"},{\"y\":1,\"x\":21,\"h\":2,\"type\":\"wall\"},{\"y\":4,\"x\":2,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":1,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":4,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":6,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":8,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":10,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":12,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":14,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":16,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":18,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":20,\"h\":1,\"type\":\"wall\"},{\"y\":4,\"x\":21,\"h\":1,\"type\":\"wall\"},{\"y\":1,\"x\":1,\"w\":1,\"h\":1,\"type\":\"toilet\"},{\"y\":1,\"x\":20,\"w\":1,\"h\":1,\"type\":\"toilet\"}]}}}"
}
}
我使用http://json2csharp.com/添加正确的类,匹配这个特定的 json。我重命名了一些类型名称,但结构和以前一样。这是类的代码:
public class Places
{
public List<string> available_places { get; set; }
}
public class TicketsData
{
public Places places { get; set; }
public string schemeId { get; set; }
public string scheme { get; set; }
}
public class TicketsRootObject
{
public TicketsData data { get; set; }
}
这是我偶然发现问题的地方:
TicketsRootObject tickets_root_obj = JsonConvert.DeserializeObject<TicketsRootObject>(
/*json string here*/);
List<string> available_tickets_str = tickets_root_obj.data.places.available_places;
available_places
字段为空,而它应该成为字符串列表"037","038","039","040","041","042","043","044","045","046","049","050","051","052"
我还尝试使用来自http://json2csharp.com/的“原样”(不重命名)类和属性名称。没有帮助。并且看起来类和属性的结构与 json 匹配,因为否则它会在解析期间给我异常或整个 RootObject 将为空。以防万一我也尝试将 available_places 类型从 更改List<string>
为List<int>
。
关于 Json 到 C# 转换的问题可能已经被问过数十亿次了,但我没有发现与这个案例相关。我在这里想念什么?