My model class is as under:
public class Details
{
public Customer customer{ get; set; }
public List<Point> points{ get; set; }
public List<Color> colors { get; set; }
}
Here is how populate my model:-
Details details = new Details();
var info = gets data from service
foreach (var item in info)
{
Points p = new Points();
p.value = item.value;
details.points= new List<Dial>();
fusionChartDetails.points.Add(p);
}
Color c1 = new Color();
c1.code = "#1";
Color c2 = new Color();
c2.code = "#2";
Color c3 = new Color();
c3.code = "#3";
details.colors = new List<Color>();
details.colors.Add(c1);
details.colors.Add(c2);
details.colors.Add(c3);
}
var final=JsonConvert.SerializeObject<details>();
Expected :json
{
"customer": {
"caption": "Customer 1"
},
"colors": {
"color": [
{
"code": "#1"
},
{
"code": "#2"
},
{
"code": "#3"
}
]
},
"points": {
"point": [
{
"value": "10"
}
]
} }
But Getting this:
{
"customer": {
"caption": "Customer 1"
},
"colors": [
{
"code": "#1"
},
{
"code": "#2"
},
{
"code": "#3"
}
],
"points": [
{
"value": "10"
}
]
}
Ther difference is that inner point and color object is not getting contructed in my json structure.How to do this?
You will need a couple of additional classes:
public class Details
{
public Customer customer { get; set; }
public Points points { get; set; }
public Colors colors { get; set; }
}
public class Points
{
public List<Point> point { get; set; }
}
public class Color
{
public List<Color> color { get; set; }
}
and then populate like this:
details.points = new Points
{
point = new List<Point>,
};
details.points.point.add(p1);
details.points.point.add(p2);
details.points.point.add(p3);
...
details.colors = new Colors
{
color = new List<Color>(),
};
details.colors.color.Add(c1);
details.colors.color.Add(c2);
details.colors.color.Add(c3);
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.