I want to create a JSON value in matrix form as shown below. I want a new line after every 10 records.
{
"defaults": [
-850,-850,-850,-850,-850,-850,-850,-850,-850,-850,
-850,-850,-500,-780,-780,-780,-780,-780,-780,-780,
-70,-70,-60,-50
]
}
The model class which I have created for JSON is given below. As the values can be of any data type, for example, boolean
or double
or string
, I have used List<object>
as the data type.
public class Parameter
{
public List<object> defaults { get; set; }
}
I have stored the values in the database as a single record. So at the time of fetching the data, I need to split it and have to store in an array.
string[] splitDefault = { (value shown in JSON) };
I have written the below given code to assign a value to the object:
Parameter para = new Parameter();
para.defaults = new List<object>();
foreach (string item in splitDefault)
{
if (paraDataType == "bool")
{
bool defaultValue = false;
bool.TryParse(item, out defaultValue);
para.defaults.Add(defaultValue);
}
else if (paraDataType == 9)
{
double defaultValue = 0;
double.TryParse(item, out defaultValue);
para.defaults.Add(defaultValue);
}
}
The JSON value I am getting is like this:
{
"defaults": [ 0, 350, 550, 800, 850, 1000, 1050, 1150, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900 ]
}