superhuman1314 superhuman1314 - 1 year ago 67
C# Question

Parse json that have uncertain number of data

How to parse json that have the Assets object is unknown?


"ClassName": "Excel",
"Teacher": "Esther",
"Student": 50,
"Aircond": 0,
"Assets": {
"Chair": 50,
"Table": 50,
"Fan": 2,
and might be more here and is unknown to me

Answer Source

You can use dynamic as Assets type:

public class RootObject
    public string ClassName { get; set; }
    public string Teacher { get; set; }
    public int Student { get; set; }
    public int Aircond { get; set; }
    public dynamic Assets { get; set; }

And then

RootObject ro = JsonConvert.DeserializeObject<RootObject>(json);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download