0

I have the following JSON and would like to deserialize it to the class structure below. I know I need to mess with the deserialization process but I don't find good examples for my use case. I also would like to use System.Text.Json.Serialization instead on Newtonsoft.Json.

{
   "warnings": {
        "123456789": [
            { .. },
            { .. }
        ],
        "987654321": [
            { .. },
            { .. }
        ],
        "893427443": [
            { .. },
            { .. }
        ],
        ...
   }
}
public class Root {
    public List<WarningArea> WarningAreas {get;set;}
}

public class WarningArea {
    public int AreaId {get;set;}
    public List<Warning> Warnings {get;set;}
}

public class Warning {
    ...
}
jwillmer
  • 3,570
  • 5
  • 37
  • 73
  • 1
    What's your question? – Andy Feb 08 '21 at 14:29
  • Your `Warnings` property should be a `Dictionary>` basically. – Jon Skeet Feb 08 '21 at 14:32
  • It's a dictionary. When those properties name are value . It's an indication of a dictionary. could you add little info about those " { .. },". – Drag and Drop Feb 08 '21 at 14:32
  • 1
    Does this answer your question? [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/questions/24536533/how-can-i-parse-a-json-string-that-would-cause-illegal-c-sharp-identifiers) – Drag and Drop Feb 08 '21 at 14:34
  • The Root object should be like : ``public class Root { public Dictionary> Warnings {get;set;} }`` – Mohammed Sajid Feb 08 '21 at 14:39
  • Note that tools like [quicktype.io](https://app.quicktype.io?share=awu8KtLhp3rDWt844LEE) will me able to see the dictionary if given a valid Json. – Drag and Drop Feb 08 '21 at 14:45
  • @JonSkeet Yes, that is what I was looking for! I did not know that the presented JSON would be interpreted into a dictionary. Thank you and the other that pointed this out. – jwillmer Feb 08 '21 at 14:48
  • Well Dictionary has an uniqueness constraint on the key. An object have the same constraint on property name. I believe that it's not in the Json spec. But many serializer enforce the constraint by turning keys into properties. My rule of thumb is if the property name is a value, it's a dictionary. – Drag and Drop Feb 08 '21 at 14:56
  • @DragandDrop: The JSON spec very strongly discourages the use of non-unique property names, and notes that when they're not unique, the results are not guaranteed. – Jon Skeet Feb 08 '21 at 15:25
  • @JonSkeet, sorry must be my poor communication/english skill. "_An object have the same constraint_" means it has uniqueness constraint. Even if the json spec only advice with a **should not** in bold. Im fully aware on that. I still have https://esdiscuss.org/topic/json-duplicate-keys, in my bookmark. My point was: it's not in the spec(spec never talk about dictionary). But object and dictionary share a constraint. So It's common for serializer to turn dictionary into object. Where keys becomes properties. – Drag and Drop Feb 08 '21 at 15:30

0 Answers0