Reputation: 958
I have a C# class
public class occ
{
public datetime from { get; set; }
public datetime to { get; set; }
public string Ktype { get; set; }
public flag bool { get; set; }
}
that I fill from an EF6.0 function coming from a stored procedure
I was asked to create for each record of that class a Json of this format
{
"Ktype": [
{
"from"
"to"
"flag"
}
]
}
I have a little experience with Json. Some folks here have helped me to catch up.
Until now I knew that the "outer" attribute (I do not know how to call it...sorry) came from the class name.
In this example the Ktype is a part of the class.
How can I Json-serialize this class and have Ktype as an outer Json attrib?
I want something like this:
{
"TRC": [{"from":"2016-02-09","to":"2016-02-16","flag":true}]
}
{
"TRX": [{"from":"2016-02-12","to":"2016-02-18","flag":false}]
}
Upvotes: 3
Views: 145
Reputation: 2447
Use SerializeObject
from newtonsoft lib:
string json = JsonConvert.SerializeObject(new occ(), Formatting.Indented);
For more information check this
Upvotes: 1
Reputation: 10071
Create a KTypeDto like this
public class KTypeDto
{
public List<KType> KTypes { get; set;}
}
public class KType
{
public Datetime From { get; set; }
public Datetime To { get; set; }
public Flag Bool { get; set; }
}
Upvotes: 0
Reputation: 13248
The following when populated and serialized yields the json
below:
public class Root
{
public List<Ktype> Ktype { get; set; } = new List<Ktype>();
}
public class Ktype
{
public DateTime from { get; set; }
public DateTime to { get; set; }
public bool flag { get; set; }
}
json:
{
"Ktype": [{
"from": "2016-01-28T14:43:10.3103658+00:00",
"to": "2016-01-28T14:43:10.3103658+00:00",
"flag": true
}]
}
Upvotes: 1
Reputation: 59
If I understand you correctly, maybe this will help you:
public class occ
{
public List<Ktype> ktype { get; set;}
public occ()
{
this.ktype = new List<Ktype>();
}
}
public class Ktype
{
public datetime from { get; set; }
public datetime to { get; set; }
public flag bool { get; set; }
}
Upvotes: 3