我已经创建了下面的类,我想要序列化成Json:
[JsonObject("user_auth")]
public class CrmAuth
{
[JsonProperty("user_name")]
public string Name { get; set; }
[JsonProperty("password")]
public string Password { get; set; }
}
我试图序列化它,像:
var serialized = JsonConvert.SerializeObject(crmAuthInstance);
创建json:
{
"user_name":"login name",
"password":"the password"
}
但是我想要的是:
"user_auth" : {
"user_name":"login value",
"password":"password value"
}
是否有一种简洁的方法来实现像这样的单个类的序列化?
test this command
var serialized = JsonConvert.SerializeObject(new{user_auth=crmAuthInstance});
我已经让它像这样工作了:
public class CrmAuthReq
{
[JsonProperty("user_auth")]
public CrmAuth Auth { get; set; }
}
public class CrmAuth
{
[JsonProperty("user_name")]
public string Name { get; set; }
[JsonProperty("password")]
public string Password { get; set; }
}