C# 如何将json属性反序列化为类属性?
我的JSON文件C# 如何将json属性反序列化为类属性?,c#,json,json.net,json-deserialization,C#,Json,Json.net,Json Deserialization,我的JSON文件 [ { "amount":"1000000.0", "check_number":1, "payment_number":5, "attachments":[ { "id":5324, "url":"http://www.example.com/", "filename":"january_receipt
[
{
"amount":"1000000.0",
"check_number":1,
"payment_number":5,
"attachments":[
{
"id":5324,
"url":"http://www.example.com/",
"filename":"january_receipt_copy.jpg"
}
]
}
]
public class Attachment
{
public int id { get; set; }
public string url { get; set; }
public string filename { get; set; }
}
public class AccountDetail
{
public string amount { get; set; }
public int check_number { get; set; }
public int payment_number { get; set; }
}
public class RootObject
{
public AccountDetail accountdetail{ get; set; }
public List<Attachment> attachments { get; set; }
}
我的班级档案
[
{
"amount":"1000000.0",
"check_number":1,
"payment_number":5,
"attachments":[
{
"id":5324,
"url":"http://www.example.com/",
"filename":"january_receipt_copy.jpg"
}
]
}
]
public class Attachment
{
public int id { get; set; }
public string url { get; set; }
public string filename { get; set; }
}
public class AccountDetail
{
public string amount { get; set; }
public int check_number { get; set; }
public int payment_number { get; set; }
}
public class RootObject
{
public AccountDetail accountdetail{ get; set; }
public List<Attachment> attachments { get; set; }
}
公共类附件
{
公共int id{get;set;}
公共字符串url{get;set;}
公共字符串文件名{get;set;}
}
公共类AccountDetail
{
公共字符串金额{get;set;}
公共整数校验号{get;set;}
公共整数{get;set;}
}
公共类根对象
{
公共AccountDetail AccountDetail{get;set;}
公共列表附件{get;set;}
}
现在我想映射JSON文件的属性'check_number'、'amount'等
通过使用newtonsoft JSON反序列化来accountdetail
您需要以下两个类:
public class Attachment
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("filename")]
public string Filename { get; set; }
}
public class AccountDetails
{
[JsonProperty("amount")]
public string Amount { get; set; }
[JsonProperty("check_number")]
public int CheckNumber { get; set; }
[JsonProperty("payment_number")]
public int PaymentNumber { get; set; }
[JsonProperty("attachments")]
public IList<Attachment> Attachments { get; set; }
}
公共类附件
{
[JsonProperty(“id”)]
公共int Id{get;set;}
[JsonProperty(“url”)]
公共字符串Url{get;set;}
[JsonProperty(“文件名”)]
公共字符串文件名{get;set;}
}
公共类帐户详细信息
{
[JsonProperty(“金额”)]
公共字符串金额{get;set;}
[JsonProperty(“检查编号”)]
公共整数校验号{get;set;}
[JsonProperty(“付款编号”)]
public int PaymentNumber{get;set;}
[JsonProperty(“附件”)]
公共IList附件{get;set;}
}
通过定义上述类,您可以按如下方式反序列化json:
var accountsDetails = JsonConvert.DeserializeObject<IEnumerable<AccountDetails>>(json);
var accountsDetails=JsonConvert.DeserializeObject(json);
您需要以下两个类:
public class Attachment
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("filename")]
public string Filename { get; set; }
}
public class AccountDetails
{
[JsonProperty("amount")]
public string Amount { get; set; }
[JsonProperty("check_number")]
public int CheckNumber { get; set; }
[JsonProperty("payment_number")]
public int PaymentNumber { get; set; }
[JsonProperty("attachments")]
public IList<Attachment> Attachments { get; set; }
}
公共类附件
{
[JsonProperty(“id”)]
公共int Id{get;set;}
[JsonProperty(“url”)]
公共字符串Url{get;set;}
[JsonProperty(“文件名”)]
公共字符串文件名{get;set;}
}
公共类帐户详细信息
{
[JsonProperty(“金额”)]
公共字符串金额{get;set;}
[JsonProperty(“检查编号”)]
公共整数校验号{get;set;}
[JsonProperty(“付款编号”)]
public int PaymentNumber{get;set;}
[JsonProperty(“附件”)]
公共IList附件{get;set;}
}
通过定义上述类,您可以按如下方式反序列化json:
var accountsDetails = JsonConvert.DeserializeObject<IEnumerable<AccountDetails>>(json);
var accountsDetails=JsonConvert.DeserializeObject(json);
显示您尝试过的代码,并告诉我们出了什么问题?@un lucky:var listRootObject=JsonConvert.DeserializeObject(response.ToS)tring());这里,我的响应等于上面显示的json文件。当我访问listRootObject.accountdetail时,它给出null,但我想将accountdetail属性映射为json文件的“check_number”和“amount”属性。显示您尝试过的代码并告诉我们出了什么问题?@un lucky:var listRootObject=JsonConvert.DeserializeObject(response.ToString());这里,我的响应等于上面显示的json文件。当我访问listRootObject.accountdetail时,它给出null,但我想将accountdetail属性映射为json文件“check_number”,“amount”属性。var listRootObject=JsonConvert.DeserializeObject(response.ToString());这里,我的响应等于上面显示的json文件。当我访问listRootObject.accountdetail时,它给出null,但我想将accountdetail属性映射为json文件“check_number”,“amount”属性。var listRootObject=JsonConvert.DeserializeObject(response.ToString());在这里,我的响应等于上面显示的json文件。当我访问listRootObject.accountdetail时,它给出null,但我想将accountdetail属性映射为json文件的“check_number”和“amount”属性。