【问题标题】:JavaScriptSerializer Deserialize Json Array of a Different KindJavaScriptSerializer 反序列化不同类型的 Json 数组
【发布时间】:2021-09-08 22:19:21
【问题描述】:

我看到以前有人问过这类问题。但是,此处发布的所有出色示例都与我的有所不同。我似乎找不到一个看起来像这样的 Json 数组的示例,并且我已经尝试了至少 2 打示例以及将编译的每种类型的反序列化代码,但是无论我尝试什么,我都无法得到这个去工作。任何帮助将不胜感激。

这是我回来的 Json。

{"Value": [
    {
        "BirthSexId": 1,
        "BloodTypeVerificationPending": false,
        "CenterPatientId": "80671472",
        "DateOfBirth": "1970-02-16T00:00:00",
        "EthnicityRace": [
            1048576
        ],
        "FirstName": "Candidate",
        "HasParentRegistration": false,
        "LastName": "323820260",
        "ListingCenterCode": "HHHH",
        "ListingCenterType": "TX1",
        "ListingDateUsedByMatchUtc": "2011-01-21T19:17:26.04Z",
        "MedicalUrgencyStatusId": 4099,
        "RegistrationAddDateUtc": "2011-01-21T19:17:26.667Z",
        "RegistrationId": 681292,
        "RegistrationInactiveReasonId": 7,
        "RegistrationOrganCode": "KI",
        "Removed": false,
        "SocialSecurityNumber": "323820260",
        "VerifiedBloodTypeCode": "A"
    },
    {
        "BirthSexId": 2,
        "BloodTypeVerificationPending": false,
        "CenterPatientId": "72240245",
        "DateOfBirth": "1974-04-17T00:00:00",
        "EthnicityRace": [
            33554432
        ],
        "FirstName": "Candidate",
        "HasParentRegistration": false,
        "LastName": "322823847",
        "ListingCenterCode": "HHHH",
        "ListingCenterType": "TX1",
        "ListingDateUsedByMatchUtc": "2019-07-01T21:13:28.61Z",
        "MedicalUrgencyStatusId": 4010,
        "RegistrationAddDateUtc": "2019-07-01T21:13:28.693Z",
        "RegistrationId": 1203041,
        "RegistrationOrganCode": "KI",
        "Removed": false,
        "SocialSecurityNumber": "322823847",
        "VerifiedBloodTypeCode": "A"
    }
],
"ValidationResults": [] }

这是我的 C# 类

public class UnosRegistration
{
    public Int32 BirthSexId { get; set; }
    public Boolean BloodTypeVerificationPending { get; set; }
    public String CenterPatientId { get; set; }
    public String DateOfBirth { get; set; }
    public Int32[] EthnicityRace { get; set; }
    public String FirstName { get; set; }
    public Boolean HasParentRegistration { get; set; }
    public String LastName { get; set; }
    public String ListingCenterCode { get; set; }
    public String ListingCenterType { get; set; }
    public String ListingDateUsedByMatchUtc { get; set; }
    public Int32 MedicalUrgencyStatusId { get; set; }
    public String RegistrationAddDateUtc { get; set; }
    public Int32 RegistrationId { get; set; }
    public Int32 RegistrationInactiveReasonId { get; set; }
    public String RegistrationOrganCode { get; set; }
    public Boolean Removed { get; set; }
    public String SocialSecurityNumber { get; set; }
    public String VerifiedBloodTypeCode { get; set; }
}

这是我的 C# 代码 sn-p

response = (HttpWebResponse)myWebRequest.GetResponse();

dynamic responseString;

responseString = new StreamReader( response.GetResponseStream() ).ReadToEnd();

List<UnosRegistration> jsonResult = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize( responseString, typeof( List<UnosRegistration> ) );

var RegID = jsonResult.FirstOrDefault().RegistrationId;

感谢您的帮助。

【问题讨论】:

    标签: c# arrays json deserialization


    【解决方案1】:

    请注意,您的 JSON 有一个 Value 键,其中包含一个 UnosRegistration 数组。告诉您的代码反序列化 UnosRegistrationList 将不起作用,因为反序列化器将在 JSON 的顶层查找数组,但它不存在。

    要解决这个问题,你需要一个包装类,这个类将包含一个名为ValueList&lt;UnosRegistration&gt; 属性,如下所示:

    public class Wrapper
    {
        public List<UnosRegistration> Value { get; set; }
    }
    

    然后你像这样反序列化

    Wrapper jsonResult = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Wrapper>(responseString);
    

    【讨论】:

    • 哦,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2012-09-20
    • 2022-01-19
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多