0

This is what I'm trying to achieve. I'm trying to serialize my model into a JSON for post request to insert records

{
    "Data": [
        {
            "employee_num": "7812345",
            "code": "333",
            "startdate": "2020-10-03"
        },
        {
            "employee_num": "2345789",
            "code": "444",
            "startdate": "2020-10-03"
        }
    ]
}

I'm stuck with this

{
     "employee_num": "7812345",
     "code": "333",
     "startdate": "2020-10-03"
},
{
     "employee_num": "2345789",
     "code": "444",
     "startdate": "2020-10-03"
}

Here is my code

var options = new JsonSerializerOptions
{
WriteIndented = false
};
var jsonString = JsonSerializer.Serialize(Model, options);

Newbie here

5
  • Can you post the model/class for Model. Commented Oct 24, 2020 at 21:03
  • What you are stuck with isn't valid JSON. You have two objects not in a collection. Try serializing new {Data = model} Commented Oct 24, 2020 at 21:07
  • Welcome to SO Ken! 2 Questions: Is the the Data element/level necessary? Do you have access to the classes used by the api you are POSTing to? Commented Oct 24, 2020 at 22:16
  • @ShaiCohen The data element is necessary because I'm using a middle ware to process/handle the API. There's no other way to submit 1 post request for the middle ware to capture number of data. Commented Oct 25, 2020 at 0:51
  • @Jawad It's like this class Model { public string employee_num { get; set; } public string code { get; set; } public string startdate{ get; set; } } Then I used List to add data public List<Model> model= new List<Model>(); Model.Add(new Model { employee_num = "1", code = "1", startdate = "1" } Commented Oct 25, 2020 at 1:14

2 Answers 2

2

I used the Json Conerter from newtonsoft and got it the format you want.

Test t = new Test("Max", "Musterallee", "[email protected]");
Test t1 = new Test("Max2", "Musterallee2", "[email protected]");

Test2 t2 = new Test2();
t2.addUser(t);
t2.addUser(t1);
var output = JsonConvert.SerializeObject(t2);
Console.WriteLine(output);

Test:

class Test
{
    public string name { get; set; }
    public string adress { get; set; }
    public string email { get; set; }
    public Test(string name, string adress, string email)
    {
        this.name = name;
        this.adress = adress;
        this.email = email;
    }
}

Test2:

class Test2
{
    public List<Test> Data;
    public Test2()
    {
        Data = new List<Test>();
    }

    public void addUser (Test t1)
    {
        Data.Add(t1);
    }
}

And the output looked like this:

{
  "Data": [
    {
      "name": "Max",
      "adress": "Musterallee",
      "email": "[email protected]"
    },
    {
      "name": "Max2",
      "adress": "Musterallee2",
      "email": "[email protected]"
    }
  ]
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the solution. Is there a way to load multiple data without creating new instance of the model? I used this way of adding the data: public List<Model> model= new List<Model>(); then adding new data in the list
Adding data like this model.Add(new Model { employee_num = "7812345", code = "444", startdate = "2020-01-01" });
0

Well, Technically, your json suggests you should have a model like this:

public partial class SomeClass // You can choose some better class names.
{
    [JsonProperty("Data")]
    public List<Datum> Data { get; set; }
    public SomeClass()
    {
        Data = new List<Datum>();
    }

}

public partial class Datum
{
    [JsonProperty("employee_num")]
    public string EmployeeNum { get; set; }

    [JsonProperty("code")]
    public string Code { get; set; }

    [JsonProperty("startdate")]
    public string Startdate { get; set; }
}

And this is how it is going to be populated:

var someClassObj = new SomeClass();

var datum = new Datum
{
    EmployeeNum = "123",
    Code = "321",
    StartDate = "2003-03-03"
};
someClassObj.Data.Add(datum);
// You can add more objects in it as per your need.

And then to serialize this to a json you should do:

var json = JsonConvert.Serialize(someClassObj);

The output will be this:

{
    "Data": [
        {
            "employee_num": "123",
            "code": "321",
            "startdate": "2003-03-03"
        }
    ]
}

2 Comments

Is it necessary to have difference instance of the model everytime I add a new data? Thank you for the solution by the way. I really appreciate it.
I got it now guys. Instead of creating new instance for the new record, I just reuse the existing then change the values and add again to the List object. Thank you for the time. Really appreciate it.

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.