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
JFRBPH12™
It's more fun in the Philippines :)
http://www.philippinetourismusa.com/
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; }
}
Member
243 Points
566 Posts
Converting JSON to Model
Jul 09, 2018 10:25 AM|johnjalani|LINK
Hi,
This is my JSON
This is the Model
And this is the Convert code
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
It's more fun in the Philippines :)
http://www.philippinetourismusa.com/
Star
8119 Points
2778 Posts
Re: Converting JSON to Model
Jul 09, 2018 10:59 AM|vahid bakkhi|LINK
hi
you have to define a class called DashboardBuild for the root object
and your complete code
and your model
I hope it can be helpful
Please MARK AS ANSWER if suggestion helps.
Member
243 Points
566 Posts
Re: Converting JSON to Model
Jul 09, 2018 12:00 PM|johnjalani|LINK
Thanks for the help
It's more fun in the Philippines :)
http://www.philippinetourismusa.com/