locked
Converting JSON to Model RRS feed

  • Question

  • User-1132163444 posted

    Hi,

    This is my JSON

    {
        "DashboardBuild": {
            "Reports": [
                {
                    "Name": "Data List",
                    "StoredProc": "api_Data_Select",
                    "Type": "Table",
                    "Data": {
                        "x": [
                            0.00,
                            0.00,
                            0.00
                        ],
                        "xField": [
                            "",
                            "",
                            ""
                        ],
                        "xLabel": [
                            "",
                            "",
                            ""
                        ],
                        "y": [
                            0.00,
                            0.00
                        ],
                        "yField": [
                            "",
                            ""
                        ],
                        "yLabel": [
                            "",
                            ""
                        ],
                        "z": [
                            0.00,
                            0.00,
                            0.00
                        ],
                        "zField": [
                            "",
                            "",
                            ""
                        ],
                        "zLabel": [
                            "null",
                            "null",
                            "null"
                        ]
                    },
                    "Order": 1,
                    "Width": 6,
                    "Enable": true
                }
            ]
        }
    }

    This is the Model

    public class DashboardBuild
        {
            [JsonProperty(PropertyName = "Reports")]
            public IEnumerable<Report> Reports { getset; }
        }
     
        public class Data
        {
            [JsonProperty(PropertyName = "x")]
            public List<decimal> x { getset; }
            [JsonProperty(PropertyName = "xField")]
            public List<string> xField { getset; }
            [JsonProperty(PropertyName = "xLabel")]
            public List<string> xLabel { getset; }
            [JsonProperty(PropertyName = "y")]
            public List<decimal> y { getset; }
            [JsonProperty(PropertyName = "yField")]
            public List<string> yField { getset; }
            [JsonProperty(PropertyName = "yLabel")]
            public List<string> yLabel { getset; }
            [JsonProperty(PropertyName = "z")]
            public List<decimal> z { getset; }
            [JsonProperty(PropertyName = "zField")]
            public List<string> zField { getset; }
            [JsonProperty(PropertyName = "zLabel")]
            public List<string> zLabel { getset; }
        }
        public class Report
        {
            [JsonProperty(PropertyName = "Name")]
            public string Name { getset; }
            [JsonProperty(PropertyName = "StoredProc")]
            public string StoredProc { getset; }
            [JsonProperty(PropertyName = "Type")]
            public string Type { getset; }
            [JsonProperty(PropertyName = "Data")]
            public Data Data { getset; }
            [JsonProperty(PropertyName = "Order")]
            public int Order { getset; }
            [JsonProperty(PropertyName = "Width")]
            public int Width { getset; }
            [JsonProperty(PropertyName = "Enable")]
            public bool Enable { getset; }
        }

    And this is the Convert code

     public DashboardBuild GetDashboard(Guid masterId)
            {
                var model = new DashboardBuild ();
    
                var path = @"c:\DashboardBuild.json";
    
                model = LoadJSON<DashboardBuild>(path);
    
                return model;
            }
    
    
     public static T LoadJSON<T>(string path) where T : class
            {
                using (StreamReader r = new StreamReader(path))
                {
                    string json = r.ReadToEnd();
                    var items = JsonConvert.DeserializeObject<T>(json);
    
                    return items;
                }
            }

    The problem is the Deserialized object "items" shows the Property Reports = NULL......

    as you can see in the JSON, there are data like Name and Type, can anyone help me know why??

    Thanks

    Monday, July 9, 2018 10:25 AM

Answers

  • User-369506445 posted

    hi

    you have to define a class called DashboardBuild for the root object

    public class RootObject
        {
            public DashboardBuild DashboardBuild { get; set; }
        }

    and your complete code

    public RootObject GetDashboard(Guid masterId)
            {
                var model = new RootObject();
                var path = @"c:\DashboardBuild.json";
                 model = LoadJSON<RootObject>(path);
    
                return model;
            }
            public static T LoadJSON<T>(string path) where T : class
            {
                using (StreamReader r = new StreamReader(path))
                {
                    string json = r.ReadToEnd();
                    var items = JsonConvert.DeserializeObject<T>(json);
    
                    return items;
                }
            }

    and your model

    public class RootObject
        {
            public DashboardBuild DashboardBuild { get; set; }
        }
    
        public class DashboardBuild
        {
            [JsonProperty(PropertyName = "Reports")]
            public IEnumerable<Report> Reports { get; set; }
        }
    
        public class Data
        {
            [JsonProperty(PropertyName = "x")]
            public List<decimal> x { get; set; }
            [JsonProperty(PropertyName = "xField")]
            public List<string> xField { get; set; }
            [JsonProperty(PropertyName = "xLabel")]
            public List<string> xLabel { get; set; }
            [JsonProperty(PropertyName = "y")]
            public List<decimal> y { get; set; }
            [JsonProperty(PropertyName = "yField")]
            public List<string> yField { get; set; }
            [JsonProperty(PropertyName = "yLabel")]
            public List<string> yLabel { get; set; }
            [JsonProperty(PropertyName = "z")]
            public List<decimal> z { get; set; }
            [JsonProperty(PropertyName = "zField")]
            public List<string> zField { get; set; }
            [JsonProperty(PropertyName = "zLabel")]
            public List<string> zLabel { get; set; }
        }
        public class Report
        {
            [JsonProperty(PropertyName = "Name")]
            public string Name { get; set; }
            [JsonProperty(PropertyName = "StoredProc")]
            public string StoredProc { get; set; }
            [JsonProperty(PropertyName = "Type")]
            public string Type { get; set; }
            [JsonProperty(PropertyName = "Data")]
            public Data Data { get; set; }
            [JsonProperty(PropertyName = "Order")]
            public int Order { get; set; }
            [JsonProperty(PropertyName = "Width")]
            public int Width { get; set; }
            [JsonProperty(PropertyName = "Enable")]
            public bool Enable { get; set; }
        }

    I hope it can be helpful

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 9, 2018 10:59 AM

All replies

  • User-369506445 posted

    hi

    you have to define a class called DashboardBuild for the root object

    public class RootObject
        {
            public DashboardBuild DashboardBuild { get; set; }
        }

    and your complete code

    public RootObject GetDashboard(Guid masterId)
            {
                var model = new RootObject();
                var path = @"c:\DashboardBuild.json";
                 model = LoadJSON<RootObject>(path);
    
                return model;
            }
            public static T LoadJSON<T>(string path) where T : class
            {
                using (StreamReader r = new StreamReader(path))
                {
                    string json = r.ReadToEnd();
                    var items = JsonConvert.DeserializeObject<T>(json);
    
                    return items;
                }
            }

    and your model

    public class RootObject
        {
            public DashboardBuild DashboardBuild { get; set; }
        }
    
        public class DashboardBuild
        {
            [JsonProperty(PropertyName = "Reports")]
            public IEnumerable<Report> Reports { get; set; }
        }
    
        public class Data
        {
            [JsonProperty(PropertyName = "x")]
            public List<decimal> x { get; set; }
            [JsonProperty(PropertyName = "xField")]
            public List<string> xField { get; set; }
            [JsonProperty(PropertyName = "xLabel")]
            public List<string> xLabel { get; set; }
            [JsonProperty(PropertyName = "y")]
            public List<decimal> y { get; set; }
            [JsonProperty(PropertyName = "yField")]
            public List<string> yField { get; set; }
            [JsonProperty(PropertyName = "yLabel")]
            public List<string> yLabel { get; set; }
            [JsonProperty(PropertyName = "z")]
            public List<decimal> z { get; set; }
            [JsonProperty(PropertyName = "zField")]
            public List<string> zField { get; set; }
            [JsonProperty(PropertyName = "zLabel")]
            public List<string> zLabel { get; set; }
        }
        public class Report
        {
            [JsonProperty(PropertyName = "Name")]
            public string Name { get; set; }
            [JsonProperty(PropertyName = "StoredProc")]
            public string StoredProc { get; set; }
            [JsonProperty(PropertyName = "Type")]
            public string Type { get; set; }
            [JsonProperty(PropertyName = "Data")]
            public Data Data { get; set; }
            [JsonProperty(PropertyName = "Order")]
            public int Order { get; set; }
            [JsonProperty(PropertyName = "Width")]
            public int Width { get; set; }
            [JsonProperty(PropertyName = "Enable")]
            public bool Enable { get; set; }
        }

    I hope it can be helpful

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 9, 2018 10:59 AM
  • User-1132163444 posted

    Thanks for the help

    Monday, July 9, 2018 12:00 PM