我想從Object寫入數據到Json文件。C#寫入Json文件錯誤
我Person類
public class Person
{
private string firstName;
private string lastName;
private int height;
private double weight;
public Person() { }
public Person(string firstName, string lastName, int height, double weight)
{
this.firstName = firstName;
this.lastName = lastName;
this.height = height;
this.weight = weight;
}
}
我的計劃類
class Program
{
static void Main(string[] args)
{
// serialize JSON to a string and then write string to a file
Person ps1 = new Person("Tay", "Son", 180, 99.99);
string json = JsonConvert.SerializeObject(ps1,Formatting.Indented);
File.WriteAllText(@"c:\person.json", json);
Console.WriteLine("Done");
Console.ReadLine();
}
}
person.json只顯示: 「{}」
請幫我解決這個錯誤。
好的,謝謝。有用 – anhtv13