简体   繁体   中英

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

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

Test t = new Test("Max", "Musterallee", "Mustermann@Muster.de");
Test t1 = new Test("Max2", "Musterallee2", "Mustermann2@Muster.de");

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": "Mustermann@Muster.de"
    },
    {
      "name": "Max2",
      "adress": "Musterallee2",
      "email": "Mustermann2@Muster.de"
    }
  ]
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM