出于某种原因,反序列化后Json中的非空字符串数组成为具有空值的类属性,而它应变为非空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将为null。
以防万一我也尝试将available_places类型从List<string>
更改为List<int>
。
关于Json到C#转换的问题可能在数十亿次之前就被问过,但我发现与这种情况无关。我在这里想念什么?
答案 0 :(得分:0)