我正在使用Newtonsoft将JSON数据反序列化为对象。C#Newtonsoft JSON映射属性到数组子项
我的JSON是这样的:
{
"id": "4aa50d01-41bd-45e3-803e-f479a948acf1",
"referenceNumber": "120064",
"status": "Application in Progress",
"borrowers": [
{
"name": "John Doe",
"type": "BORROWER"
},
{
"name": "Jane Doe",
"type": "COBORROWER"
}
],
"propertyAddress": {
"zipCodePlusFour": ""
}
}
借款人阵列最多可以有2项。 1与type == "BORROWER"
和另一个与type == "COBORROWER"
我有一个LoanItem类我反序列化。
public class LoanItem
{
public string referenceNumber { get; set; }
public string status { get; set; }
}
我知道我可以用JSONProperty属性标记LoanItem财产,但我不知道是否有一种方法,我可以用一个条件添加一个数组子项。
东西也许像
[JSONProperty("borrowers[WHERE type = 'BORROWER'].name")]
public string BorrowerName { get; set; }
[JSONProperty("borrowers[WHERE type = 'COBORROWER'].name")]
public string CoBorrowerName { get; set; }
这可能吗?我可以使用JSONProperty属性吗?
不,这不是它是如何工作的。 –
@JeffMercado所以我只需要手动解析JSON并映射到我的对象? – James
你让它听起来像是一件坏事。 'JsonPropertyAttribute'不是按照你想要的方式来设计的。这是将简单的json属性映射到.net属性的一种方法。你提出的要比任何API提供的要复杂得多。如果你想要更复杂的逻辑,你必须成为提供它的人。 –