KBagaipo
KBagaipo

Reputation: 15

Serialize JSON to JSON Array (Nested JSON) for POST Request C#

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

Upvotes: 0

Views: 681

Answers (2)

Jamshaid K.
Jamshaid K.

Reputation: 4547

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

Upvotes: 0

Triims
Triims

Reputation: 75

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

Upvotes: 2

Related Questions