如何将错误的Json固定为将字符串数组的C#反序列化为具有null值的属性?

时间:2019-04-25 06:50:31

标签: c# json-deserialization json2csharp

出于某种原因,反序列化后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#转换的问题可能在数十亿次之前就被问过,但我发现与这种情况无关。我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

最终,由于Jon Skeet的评论,我刚刚将拉丁语'A'的字母更改为西里尔字母'А',并且有效。

但是我仍然遇到相同的问题,但是原因不同: 有时,来自该网站的json属性名称将在同一位置带有字母'А',然后带有字母'Б'(下一个西里尔字母)。因此,我最终使用了多个json名称分配给同一C#类属性,例如here

我还决定确保能够处理整个字母,而不必担心这个问题,因为那里没有出现多少个字母(我不在乎exect json属性名,只关心里面的数据)。所以我写了一些python代码,遍历unicode西里尔字母,为整个大写字母生成C#类。对某人来说可能听起来很愚蠢,但是对我来说效果很好。