简体   繁体   中英

How to fix wrong Json to C# deserialization of array of strings into property with null value?

For some reason non-empty array of strings in Json after deserialization becomes class property with null value, while it should become non-empty List<string> .

Here is 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\"}]}}}"}}

Same json, but parsed (for better readability):

{
  "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\"}]}}}"
  }
}

I used http://json2csharp.com/ to add correct classes, that match this particular json. I renamed some type names, but the structure is as it was before. Here is the code of classes:

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; }
}

And here is where I stumble upon the problem:

TicketsRootObject tickets_root_obj = JsonConvert.DeserializeObject<TicketsRootObject>(
                /*json string here*/);
List<string> available_tickets_str = tickets_root_obj.data.places.available_places;

available_places field is null, while it should become the list of strings "037","038","039","040","041","042","043","044","045","046","049","050","051","052"

I tried also to use classes and properties names "as is" (without renaming) from http://json2csharp.com/ . Didn't help. And looks like the structure of classes and properties matches json, because otherwise it would give me exception during parsing or whole RootObject would be null. Just in case I tried also to change available_places type from List<string> to List<int> .

Questions about Json to C# conversion have been asked before probably billion of times, but I didn't find relevant to this case. What am I missing here?

Eventually thanks to Jon Skeet 's comment I just changed latin 'A' letter to cyrillic 'А' and it worked.

However I was still gettings same problem, but for different reason: sometimes json property name from that website would come with letter 'А' , then with letter 'Б' (next cyrillic letter in alphabet) at the same place. So I ended up with using multiple json names assignment to same C# class property like here .

I also decided to make sure that I'll be able to handle whole alphabet and forget about this issue no metter how many letters occur there (I didn't care about exect json property name, only data inside). So I wrote some python code, iterating through unicode cyrillic alphabet, to generate C# class for whole uppercase alphabet. Might sound stupid to somebody, but it worked perfectly for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM