如何仅将 JSON 数据的某些属性反序列化为 C# 中的列表
How to deserialize only certain properties of JSON data into list in C#
我有以下 JSON 数据,其中包含许多我想要反序列化的属性
{
"Senders": [
{
"Id": 63465,
"Title": null,
"Firstname": "King",
"Lastname": "Kong",
"Site": {
"BSNR": "521112354",
"Name": "Irgendwo",
"Address": {
"Street": null,
"CountryCode": null,
"Zip": null,
"City": null,
"AddressSupplement": null
}
},
"SpecialField": null,
"CustomerNo": "KINGKONG",
"Contact": {
"Email": null,
"Phone": "1234",
"Mobile": null,
"FaxNumbersInfos": [
{
"Id": 324,
"Fax": "044111111",
"Description": "Fax 1"
}
]
},
"KbvChecksum": null,
"OrderEntry": {
"OrderLabelCount": 0,
"NumberRange": {
"Id": 9,
"Part": null
},
"OrderNumberAllocationType": 1,
"LastOrderNumber": 1005
},
"LaboratoryId": 190,
"Note": null,
"CreatedOn": "2017-11-29T10:11:05",
"Fullname": "King Kong",
"PersonStatus": null,
"SetTakingDateOnOrderApprove": false,
"IsMemberOfHealthInsuranceCollaborativeLaboratory": false,
"IsMemberOfPrivateCollaborativeLaboratory": true,
"HasPvsApproval": false,
"WouldLikeToIgelInPrivateLg": false,
"ShowPathoValuesFromLastResult": true,
"UsesDigitalPatterns": true,
"AllowPrintAdditionalLabels": true,
"AllowPrintCostEstimate": false,
"CostEstimateCount": 0,
"AllowMultipleFaxNumbers": true
},
{
"Id": 32222,
"Title": "Herr",
"Firstname": "Garfield",
"Lastname": "Lasagne",
"Site": {
"BSNR": "198533333",
"Name": "test",
"Address": {
"Street": "Test Str. 32 ",
"CountryCode": null,
"Zip": null,
"City": "33333 Test - Test",
"AddressSupplement": null
}
},
"SpecialField": " FA f. Allgemeinmedizin",
"CustomerNo": "KNOR",
"Contact": {
"Email": null,
"Phone": "0233333333",
"Mobile": null,
"FaxNumbersInfos": [
{
"Id": 284,
"Fax": "12345",
"Description": null
},
{
"Id": 285,
"Fax": "1235213",
"Description": null
},
{
"Id": 286,
"Fax": "2352352",
"Description": null
},
{
"Id": 311,
"Fax": "232352",
"Description": null
},
{
"Id": 322,
"Fax": "534623",
"Description": null
}
]
},
"KbvChecksum": null,
"OrderEntry": {
"OrderLabelCount": 0,
"NumberRange": {
"Id": null,
"Part": null
},
"OrderNumberAllocationType": 0,
"LastOrderNumber": null
},
"LaboratoryId": 196,
"Note": null,
"CreatedOn": "2017-03-30T08:26:03",
"Fullname": "Garfield Lasagne",
"PersonStatus": null,
"SetTakingDateOnOrderApprove": false,
"IsMemberOfHealthInsuranceCollaborativeLaboratory": false,
"IsMemberOfPrivateCollaborativeLaboratory": false,
"HasPvsApproval": false,
"WouldLikeToIgelInPrivateLg": false,
"ShowPathoValuesFromLastResult": false,
"UsesDigitalPatterns": false,
"AllowPrintAdditionalLabels": false,
"AllowPrintCostEstimate": false,
"CostEstimateCount": 0,
"AllowMultipleFaxNumbers": true
},
{
"Id": 32904,
"Title": "Dr.",
"Firstname": "Test",
"Lastname": "Test",
"Site": {
"BSNR": null,
"Name": "Dr. Test",
"Address": {
"Street": null,
"CountryCode": null,
"Zip": null,
"City": null,
"AddressSupplement": null
}
},
"SpecialField": null,
"CustomerNo": "SK",
"Contact": {
"Email": null,
"Phone": null,
"Mobile": null,
"FaxNumbersInfos": []
},
"KbvChecksum": null,
"OrderEntry": {
"OrderLabelCount": 0,
"NumberRange": {
"Id": 9,
"Part": null
},
"OrderNumberAllocationType": 1,
"LastOrderNumber": 2016
},
"LaboratoryId": 190,
"Note": null,
"CreatedOn": "2020-07-23T14:06:40",
"Fullname": "Seb Kob",
"PersonStatus": null,
"SetTakingDateOnOrderApprove": false,
"IsMemberOfHealthInsuranceCollaborativeLaboratory": false,
"IsMemberOfPrivateCollaborativeLaboratory": true,
"HasPvsApproval": false,
"WouldLikeToIgelInPrivateLg": false,
"ShowPathoValuesFromLastResult": true,
"UsesDigitalPatterns": true,
"AllowPrintAdditionalLabels": true,
"AllowPrintCostEstimate": false,
"CostEstimateCount": 0,
"AllowMultipleFaxNumbers": false
}
]
}
我尝试通过创建一个具有所有属性的对象 SenderInfo 来反序列化所有内容并尝试
var result = JsonConvert.DeserializeObject<List<SenderInfo>>(jsonString);
但我得到了 Newtonsoft.Json.JsonSerializationException。因为我不需要所有数据,所以我也更愿意只反序列化某些属性。我确定一定有办法实现这一点,也许是通过 StringReader 或其他方式?
感谢任何想法。
无论您在 SenderInfo
中有什么,List<SenderInfo>>
都不代表您的 json 结构,请尝试这样的操作:
public class SendersContainer
{
[JsonProperty("Senders")]
public List<SenderInfo> Senders { get; set; }
}
var result = JsonConvert.DeserializeObject<SendersContainer>(jsonString);
我有以下 JSON 数据,其中包含许多我想要反序列化的属性
{
"Senders": [
{
"Id": 63465,
"Title": null,
"Firstname": "King",
"Lastname": "Kong",
"Site": {
"BSNR": "521112354",
"Name": "Irgendwo",
"Address": {
"Street": null,
"CountryCode": null,
"Zip": null,
"City": null,
"AddressSupplement": null
}
},
"SpecialField": null,
"CustomerNo": "KINGKONG",
"Contact": {
"Email": null,
"Phone": "1234",
"Mobile": null,
"FaxNumbersInfos": [
{
"Id": 324,
"Fax": "044111111",
"Description": "Fax 1"
}
]
},
"KbvChecksum": null,
"OrderEntry": {
"OrderLabelCount": 0,
"NumberRange": {
"Id": 9,
"Part": null
},
"OrderNumberAllocationType": 1,
"LastOrderNumber": 1005
},
"LaboratoryId": 190,
"Note": null,
"CreatedOn": "2017-11-29T10:11:05",
"Fullname": "King Kong",
"PersonStatus": null,
"SetTakingDateOnOrderApprove": false,
"IsMemberOfHealthInsuranceCollaborativeLaboratory": false,
"IsMemberOfPrivateCollaborativeLaboratory": true,
"HasPvsApproval": false,
"WouldLikeToIgelInPrivateLg": false,
"ShowPathoValuesFromLastResult": true,
"UsesDigitalPatterns": true,
"AllowPrintAdditionalLabels": true,
"AllowPrintCostEstimate": false,
"CostEstimateCount": 0,
"AllowMultipleFaxNumbers": true
},
{
"Id": 32222,
"Title": "Herr",
"Firstname": "Garfield",
"Lastname": "Lasagne",
"Site": {
"BSNR": "198533333",
"Name": "test",
"Address": {
"Street": "Test Str. 32 ",
"CountryCode": null,
"Zip": null,
"City": "33333 Test - Test",
"AddressSupplement": null
}
},
"SpecialField": " FA f. Allgemeinmedizin",
"CustomerNo": "KNOR",
"Contact": {
"Email": null,
"Phone": "0233333333",
"Mobile": null,
"FaxNumbersInfos": [
{
"Id": 284,
"Fax": "12345",
"Description": null
},
{
"Id": 285,
"Fax": "1235213",
"Description": null
},
{
"Id": 286,
"Fax": "2352352",
"Description": null
},
{
"Id": 311,
"Fax": "232352",
"Description": null
},
{
"Id": 322,
"Fax": "534623",
"Description": null
}
]
},
"KbvChecksum": null,
"OrderEntry": {
"OrderLabelCount": 0,
"NumberRange": {
"Id": null,
"Part": null
},
"OrderNumberAllocationType": 0,
"LastOrderNumber": null
},
"LaboratoryId": 196,
"Note": null,
"CreatedOn": "2017-03-30T08:26:03",
"Fullname": "Garfield Lasagne",
"PersonStatus": null,
"SetTakingDateOnOrderApprove": false,
"IsMemberOfHealthInsuranceCollaborativeLaboratory": false,
"IsMemberOfPrivateCollaborativeLaboratory": false,
"HasPvsApproval": false,
"WouldLikeToIgelInPrivateLg": false,
"ShowPathoValuesFromLastResult": false,
"UsesDigitalPatterns": false,
"AllowPrintAdditionalLabels": false,
"AllowPrintCostEstimate": false,
"CostEstimateCount": 0,
"AllowMultipleFaxNumbers": true
},
{
"Id": 32904,
"Title": "Dr.",
"Firstname": "Test",
"Lastname": "Test",
"Site": {
"BSNR": null,
"Name": "Dr. Test",
"Address": {
"Street": null,
"CountryCode": null,
"Zip": null,
"City": null,
"AddressSupplement": null
}
},
"SpecialField": null,
"CustomerNo": "SK",
"Contact": {
"Email": null,
"Phone": null,
"Mobile": null,
"FaxNumbersInfos": []
},
"KbvChecksum": null,
"OrderEntry": {
"OrderLabelCount": 0,
"NumberRange": {
"Id": 9,
"Part": null
},
"OrderNumberAllocationType": 1,
"LastOrderNumber": 2016
},
"LaboratoryId": 190,
"Note": null,
"CreatedOn": "2020-07-23T14:06:40",
"Fullname": "Seb Kob",
"PersonStatus": null,
"SetTakingDateOnOrderApprove": false,
"IsMemberOfHealthInsuranceCollaborativeLaboratory": false,
"IsMemberOfPrivateCollaborativeLaboratory": true,
"HasPvsApproval": false,
"WouldLikeToIgelInPrivateLg": false,
"ShowPathoValuesFromLastResult": true,
"UsesDigitalPatterns": true,
"AllowPrintAdditionalLabels": true,
"AllowPrintCostEstimate": false,
"CostEstimateCount": 0,
"AllowMultipleFaxNumbers": false
}
]
}
我尝试通过创建一个具有所有属性的对象 SenderInfo 来反序列化所有内容并尝试
var result = JsonConvert.DeserializeObject<List<SenderInfo>>(jsonString);
但我得到了 Newtonsoft.Json.JsonSerializationException。因为我不需要所有数据,所以我也更愿意只反序列化某些属性。我确定一定有办法实现这一点,也许是通过 StringReader 或其他方式?
感谢任何想法。
无论您在 SenderInfo
中有什么,List<SenderInfo>>
都不代表您的 json 结构,请尝试这样的操作:
public class SendersContainer
{
[JsonProperty("Senders")]
public List<SenderInfo> Senders { get; set; }
}
var result = JsonConvert.DeserializeObject<SendersContainer>(jsonString);