C# 如何在C中仅将JSON数据的某些属性反序列化到列表中#
我有下面的JSON数据,其中包含许多我想要反序列化的属性C# 如何在C中仅将JSON数据的某些属性反序列化到列表中#,c#,json,json-deserialization,C#,Json,Json Deserialization,我有下面的JSON数据,其中包含许多我想要反序列化的属性 { "Senders": [ { "Id": 63465, "Title": null, "Firstname": "King", "Lastname": "Kong", "Site": { &
{
"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);
var result=JsonConvert.DeserializeObject(jsonString);
但是我得到了一个Newtonsoft.Json.JsonSerializationException。因为我不需要所有的数据,所以我也更喜欢只反序列化某些属性。我相信一定有办法做到这一点,也许是通过StringReader或其他什么
谢谢您的建议。无论您在
SenderInfo
中有什么内容,列表>
并不代表您的json结构,请尝试以下操作:
public class SendersContainer
{
[JsonProperty("Senders")]
public List<SenderInfo> Senders { get; set; }
}
var result = JsonConvert.DeserializeObject<SendersContainer>(jsonString);
公共类发送者容器
{
[JsonProperty(“发件人”)]
公共列表发送者{get;set;}
}
var result=JsonConvert.DeserializeObject(jsonString);
请添加SenderInfo
类。@YamgoPango很乐意帮忙!