Reputation: 405
I have a class named clsTest
which is defined as:
public class clsTest
{
public string Name;
public string Family;
public int Age;
}
I have another class named clsMain
which is Serializing three instances of clsTest
class to JSON as:
public class clsMain
{
public string mtdMain()
{
clsTest ct1_a = new clsTest();
clsTest ct1_b = new clsTest();
clsTest ct1_c = new clsTest();
ct1_a.Name = "Satoshi";
ct1_a.Family = "Nakamato";
ct1_b.Name = "Charles";
ct1_b.Family = "Hoskinson";
ct1_b.Age = 33;
ct1_c.Name = "AmirAli";
ct1_c.Family = "Sam";
ct1_c.Age = 25;
List<clsTest> lst = new List<clsTest>();
lst.Add(ct1_a);
lst.Add(ct1_b);
lst.Add(ct1_c);
JsonSerializerOptions option = new JsonSerializerOptions();
option.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
option.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
return JsonSerializer.Serialize(lst, option);
}
}
When I debug the project my list is full as shown in the screenshot:
But at the end return JsonSerializer.Serialize(lst, option);
serialize as below:
I couldn't find the problem, any suggestion will be appreciated. Thanks.
Upvotes: 1
Views: 339
Reputation: 697
Use properties instead of fields, like so:
public class Test
{
public string Name { get; set; }
public string Family { get; set; }
public int Age { get; set; }
}
Upvotes: 3