Reputation: 1338
I have a list of data points, as defined below:
public class Point {
string Rate;
string Date;
string Number;
public Point(string Rate, string Date, string Number)
{
this.Rate = Rate;
this.Date = Date;
this.Number = Number;
}
}
Then within my code I have:
List<Point> points = populatedList;
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
string text = javaScriptSerializer.Serialize(points);
System.IO.File.WriteAllText(@"C:\Users\Public\WriteText.txt", text);
When I go to view "WriteText.txt", however, all I have is a bunch of empty brackets: {}, {}, {} ...
I have also tried doing this with only one point, and then I am left with only one matching pair of brackets. I then tried serializing a string object alone and that worked fine. Why is the JavaScriptSerializer
not behaving as expected?
Upvotes: 5
Views: 4192
Reputation: 82287
The access level for class members and struct members, including nested classes and structs, is private by default. - Access ModifiersMSDN
As a result of that, the serialization will not see those properties. In order for them to be serialized, they need to be marked as public. They also need to have a public getter in order for the serializer to read the property.
public string Rate { get; set; }
public string Date { get; set; }
public string Number { get; set; }
Upvotes: 18