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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I used the Json Conerter from newtonsoft and got it the format you want.
Test t = new Test("Max", "Musterallee", "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6429111710011609050a0a242911171001164a0001">[email protected]</a>");
Test t1 = new Test("Max2", "Musterallee2", "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9bd6eee8effee9f6faf5f5a9dbd6eee8effee9b5fffe">[email protected]</a>");
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": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="feb38b8d8a9b8c939f9090beb38b8d8a9b8cd09a9b">[email protected]</a>"
},
{
"name": "Max2",
"adress": "Musterallee2",
"email": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f023a3c3b2a3d222e21217d0f023a3c3b2a3d612b2a">[email protected]</a>"
}
]
}
Method 2
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"
}
]
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0