3

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}]
}
6
  • 4
    why has ktype become an array (Its just a string in the class)? How do you map between the class and the required json? Commented Jan 28, 2016 at 14:38
  • 1
    What serializer do you use? Commented Jan 28, 2016 at 14:38
  • 1
    @James ISO 8601 - not a problem. I can handle that. Commented Jan 28, 2016 at 14:41
  • @jamiec We have that set of data (the class set) for providing information. One customer asked the above transformation in his json result. Commented Jan 28, 2016 at 14:43
  • 2
    You should create types that match the json format and either only use those or map to those, then create json from those types, this would make it easier to handle the one property becoming an array. Commented Jan 28, 2016 at 14:44

4 Answers 4

3

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; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Luca but I think that in your example ktype data is nowhere to be found. ktype is a string and its data should exist in the json.
1

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
    }]
}

Comments

1

Use SerializeObject from newtonsoft lib:

string json = JsonConvert.SerializeObject(new occ(), Formatting.Indented);

For more information check this

Comments

0

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; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.