我的实体是这样的:
class Address
{
public string Number { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Address PostalAddress { get; set; }
}
Person newPerson =
new Person()
{
Name = "Kushan",
Age = 25,
PostalAddress =
new Address()
{
Number = "No 25",
Street = "Main Street",
City = "Matale",
Country = "Sri Lanka"
}
};
现在我想将这个newPerson对象映射成JSON对象,就像这样,
{
"PER_NAME" : "Kushan",
"PER_AGE" : "25",
"PER_ADDRESS" : {
"ADD_NUMBER" : "No 25",
"ADD_STREET" : "Main Street",
"ADD_CITY" : "Matale",
"ADD_COUNTRY" : "Sri Lanka"
}
}
注意:上面只是一个示例。
我所需要的是,在序列化时自定义密钥。默认情况下,它会将属性名称作为密钥。我无法更改属性名称。怎么做?
此外,有可能改变JSON对象中键值对出现的顺序吗?
{"Age":25,"Name":"Kushan","PostalAddress":{"City":"Matale","Country":"Sri Lanka","Number":"No 25","Street":"Main Street"}}
映射到类中,那么怎么做呢? - Chintan