c# - 使用不同类型的节点从 JSON 创建类

标签 c# json serialization

我有一个 JSON 字符串,它有 3 种不同类型的节点,自动生成器不允许我处理它。

如何将此结构构建到一个类中以进行反序列化?

{
    "answers": [
        {
            "answerText": "No",
            "section": "52.209-5.a.1.i.A"
        },
        {
            "answerText": "No",
            "section": "52.209-5.a.1.i.B"
        },
        {
            "answerText": "No",
            "section": "52.209-5.a.1.i.D"
        },
        {
            "answerText": "No",
            "section": "52.209-5.a.1.i.C"
        },
        {
            "answerText": "No",
            "section": "52.209-5.a.1.ii"
        }
    ],
    "id": "FAR 52.209-5"
},
{
    "answers": {
        "SamPointOfContact": {
            "firstName": "Catherine",
            "lastName": "Kuenzel",
            "title": "VP of Public Sector US Enterprise Services"
        },
        "section": "52.203-2.b.2.i"
    },
    "id": "FAR 52.203-2"
},
{
"answers": [
{
    "section": "52.212-3.c",
    "naics": [
        {
            "naicsName": "Other Computer Related Services",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "ExcpCounter": 1,
            "isSmallBusiness": "N",
            "naicsCode": 541519
        },
        {
            "naicsName": "Information Technology Value Added Resellers",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "ExcpCounter": 2,
            "isSmallBusiness": "N",
            "naicsCode": 541519
        },
        {
            "naicsName": "Engineering Services",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "ExcpCounter": 1,
            "isSmallBusiness": "N",
            "naicsCode": 541330
        },
        {
            "naicsName": "Military and Aerospace Equipment and Military Weapons",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "ExcpCounter": 2,
            "isSmallBusiness": "N",
            "naicsCode": 541330
        },
        {
            "naicsName": "Contracts and Subcontracts for Engineering Services Awarded Under the National Energy Policy Act of 1992",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "ExcpCounter": 3,
            "isSmallBusiness": "N",
            "naicsCode": 541330
        },
        {
            "naicsName": "Marine Engineering and Naval Architecture",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "ExcpCounter": 4,
            "isSmallBusiness": "N",
            "naicsCode": 541330
        },
        {
            "naicsName": "Software Publishers",
            "hasSizeChanged": "null",
            "isPrimary": true,
            "isSmallBusiness": "N",
            "naicsCode": 511210
        },
        {
            "naicsName": "Wired Telecommunications Carriers",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "isSmallBusiness": "N",
            "naicsCode": 517311
        },
        {
            "naicsName": "Data Processing, Hosting, and Related Services",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "isSmallBusiness": "N",
            "naicsCode": 518210
        },
        {
            "naicsName": "Custom Computer Programming Services",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "isSmallBusiness": "N",
            "naicsCode": 541511
        },
        {
            "naicsName": "Computer Systems Design Services",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "isSmallBusiness": "N",
            "naicsCode": 541512
        },
        {
            "naicsName": "Computer Facilities Management Services",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "isSmallBusiness": "N",
            "naicsCode": 541513
        },
        {
            "naicsName": "Other Scientific and Technical Consulting Services",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "isSmallBusiness": "N",
            "naicsCode": 541690
        },
        {
            "naicsName": "All Other Professional, Scientific, and Technical Services",
            "hasSizeChanged": "null",
            "isPrimary": false,
            "isSmallBusiness": "N",
            "naicsCode": 541990
        }
    ]
}

据我所知,每个答案都会有:

部分

以及以下其中一项: 答案文本、SamPointOfContact 或 NAICS

来自 API 的完整 JSON 字符串:

{"sam_data":{"registration":{"pscCodes":"null","dodaac":"null","pastPerformancePoc":{"nonUsPhone":"null","firstName":"MELISSA","lastName":"RANSLEM","middleInitial":"null","address":{"zip":"20190","city":"Reston","countryCode":"USA","stateorProvince":"VA","line2":"null","line1":"12012 Sunset Hills RD","zipPlus4":"null"},"notes":"null","usPhoneExt":"null","title":"null","fax":"null","usPhone":"5713532701","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c1119100e1c11151f0e130f131a08721f1311" rel="noreferrer noopener nofollow">[email protected]</a>"},"lastUpdateDate":"2019-10-02 11:42:00.2","businessStartDate":"1981-06-25","congressionalDistrict":"WA 01","corporateUrl":"http://www.microsoft.com","samAddress":{"zip":"98052","city":"Redmond","countryCode":"USA","stateorProvince":"WA","line2":"null","line1":"1 Microsoft Way","zipPlus4":"8300"},"electronicBusinessPoc":{"nonUsPhone":"null","firstName":"MELISSA","lastName":"RANSLEM","middleInitial":"null","address":{"zip":"20190","city":"RESTON","countryCode":"USA","stateorProvince":"VA","line2":"null","line1":"12012 SUNSET HILLS ROAD","zipPlus4":"null"},"notes":"null","usPhoneExt":"null","title":"CONTRACTS MANAGER","fax":"4257086748","usPhone":"5713532701","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3eee6eff1e3eeeae0f1ecf0ece5f78de0ecee" rel="noreferrer noopener nofollow">[email protected]</a>"},"corporateStructureCode":"2L","certificationsURL":{"pdfUrl":"https://sam.gov/SAM/filedownload?reportType=2&orgId=cjtAmfDzDsaKTO8U4n1UKUa2KrXbqstk%2BO0iASuNgvum%2B4NlFkRiGprZhIF96imb&pitId=49aNpMKWuSvejCJ3Wvi2DUfJJpLdlhT66P77hYyLHlRhcYGmjXFFJHD2XTuRV2vTUaRmz8pvfeEH%0Av7KcTAhZQg%3D%3D&requestId=mAs0396QKDkznCr"},"bondingInformation":{"serviceAggregate":"null","servicePerContract":"null","constructionAggregate":"null","constructionPerContract":"null"},"dunsPlus4":"0000","correspondenceFlag":"null","debtSubjectToOffset":false,"qualifications":{"pdfUrl":"null","acass":{"answers":{"answerText":"No","section":"SF330.1"},"id":"SF330"}},"cage":"60128","fiscalYearEndCloseDate":"06/30","publicDisplay":true,"ncage":"null","altGovtBusinessPoc":{"nonUsPhone":"null","firstName":"MELISSA","lastName":"RANSLEM","middleInitial":"null","address":{"zip":"20190","city":"RESTON","countryCode":"USA","stateorProvince":"VA","line2":"null","line1":"12012 SUNSET HILLS ROAD","zipPlus4":"null"},"notes":"null","usPhoneExt":"null","title":"null","fax":"null","usPhone":"5713532701","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d39e969f81939e9a90819c809c9587fd909c9e" rel="noreferrer noopener nofollow">[email protected]</a>"},"registrationDate":"2001-05-24 00:00:00.0","govtBusinessPoc":{"nonUsPhone":"null","firstName":"MELISSA","lastName":"RANSLEM","middleInitial":"null","address":{"zip":"20190","city":"RESTON","countryCode":"USA","stateorProvince":"VA","line2":"null","line1":"12012 SUNSET HILLS ROAD","zipPlus4":"null"},"notes":"null","usPhoneExt":"null","title":"CONTRACTS MANAGER","fax":"4257086748","usPhone":"5713532701","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2fff7fee0f2fffbf1e0fde1fdf4e69cf1fdff" rel="noreferrer noopener nofollow">[email protected]</a>"},"expirationDate":"2020-10-01 10:48:36.71","altElectronicBusinessPoc":{"nonUsPhone":"null","firstName":"MELISSA","lastName":"RANSLEM","middleInitial":"null","address":{"zip":"20190","city":"RESTON","countryCode":"USA","stateorProvince":"VA","line2":"null","line1":"12012 SUNSET HILLS ROAD","zipPlus4":"null"},"notes":"null","usPhoneExt":"null","title":"null","fax":"null","usPhone":"5713532701","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fcb1b9b0aebcb1b5bfaeb3afb3baa8d2bfb3b1" rel="noreferrer noopener nofollow">[email protected]</a>"},"purposeOfRegistration":"ALL_AWARDS","companyDivision":"null","financialAssistanceResponse":"No","submissionDate":"2019-10-02 10:48:36.7","creditCardUsage":true,"countryOfIncorporation":"USA","businessTypes":["2X","MF"],"certifications":{"pdfUrl":"https://sam.gov/SAM/filedownload?reportType=2&orgId=U8BeO3GBdtx5C03t2ZZrh09e%2BMOgCT%2F5q6WqC%2BSik3qQgeriyZFR3EyVmYmJJ8KN&pitId=XU%2BuEDYLvXBbO0%2BOG2x1eK2wC0FT4ocU%2Febr%2FvqOZWsbEd5NjPqoozGVWZIGLYHtToL8xJfOkUfk%0A%2BaJmd2jnmw%3D%3D&requestId=FCwZSoGN9o0tAc6","farResponses":[{"answers":[{"answerText":"No","section":"52.209-2.c.1"},{"answerText":"No","section":"52.209-2.c.2"}],"id":"FAR 52.209-2"},{"answers":[{"answerText":"No","section":"52.209-5.a.1.i.A"},{"answerText":"No","section":"52.209-5.a.1.i.B"},{"answerText":"No","section":"52.209-5.a.1.i.D"},{"answerText":"No","section":"52.209-5.a.1.i.C"},{"answerText":"No","section":"52.209-5.a.1.ii"}],"id":"FAR 52.209-5"},{"answers":{"SamPointOfContact":{"firstName":"Catherine","lastName":"Kuenzel","title":"VP of Public Sector US Enterprise Services"},"section":"52.203-2.b.2.i"},"id":"FAR 52.203-2"},{"answers":{"answerText":"No","section":"52.215-6.a"},"id":"FAR 52.215-6"},{"answers":{"answerText":"No","section":"52.214-14.a"},"id":"FAR 52.214-14"},{"answers":{"answerText":"Vendor will provide information with specific offers to the Government","section":"52.223-4"},"id":"FAR 52.223-4"},{"answers":{"answerText":"Vendor will provide information with specific offers to the Government","section":"52.223-9"},"id":"FAR 52.223-9"},{"answers":{"answerText":"No","section":"52.219-2.a"},"id":"FAR 52.219-2"},{"answers":[{"answerText":"TIN ON FILE","section":"52.204-3.d"},{"answerText":"Corporate Entity (Not Tax Exempt)","section":"52.204-3.e"},{"answerText":"No","section":"52.204-3.f"}],"id":"FAR 52.204-3"},{"answers":{"answerText":"No","section":"52.223-22.a"},"id":"FAR 52.223-22"},{"answers":[{"section":"52.212-3.c","naics":[{"naicsName":"Other Computer Related Services","hasSizeChanged":"null","isPrimary":false,"ExcpCounter":1,"isSmallBusiness":"N","naicsCode":541519},{"naicsName":"Information Technology Value Added Resellers","hasSizeChanged":"null","isPrimary":false,"ExcpCounter":2,"isSmallBusiness":"N","naicsCode":541519},{"naicsName":"Engineering Services","hasSizeChanged":"null","isPrimary":false,"ExcpCounter":1,"isSmallBusiness":"N","naicsCode":541330},{"naicsName":"Military and Aerospace Equipment and Military Weapons","hasSizeChanged":"null","isPrimary":false,"ExcpCounter":2,"isSmallBusiness":"N","naicsCode":541330},{"naicsName":"Contracts and Subcontracts for Engineering Services Awarded Under the National Energy Policy Act of 1992","hasSizeChanged":"null","isPrimary":false,"ExcpCounter":3,"isSmallBusiness":"N","naicsCode":541330},{"naicsName":"Marine Engineering and Naval Architecture","hasSizeChanged":"null","isPrimary":false,"ExcpCounter":4,"isSmallBusiness":"N","naicsCode":541330},{"naicsName":"Software Publishers","hasSizeChanged":"null","isPrimary":true,"isSmallBusiness":"N","naicsCode":511210},{"naicsName":"Wired Telecommunications Carriers","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":517311},{"naicsName":"Data Processing, Hosting, and Related Services","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":518210},{"naicsName":"Custom Computer Programming Services","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":541511},{"naicsName":"Computer Systems Design Services","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":541512},{"naicsName":"Computer Facilities Management Services","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":541513},{"naicsName":"Other Scientific and Technical Consulting Services","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":541690},{"naicsName":"All Other Professional, Scientific, and Technical Services","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":541990}]},{"answerText":"No","section":"52.212-3.q.2.i"},{"answerText":"No","section":"52.212-3.q.2.ii"},{"answerText":"No","section":"52.212-3.c.11.ii"},{"answerText":"Yes","section":"52.212-3.d.1.i"},{"answerText":"Yes","section":"52.212-3.d.1.ii"},{"answerText":"Microsoft Corporation  has developed and has on file affirmative action programs required by Secretary of Labor regulations.","section":"52.212-3.d.2.i"},{"answerText":"Vendor will provide information with specific offers to the Government","section":"52.212-3.f"},{"answerText":"No","section":"52.212-3.h.1"},{"answerText":"No","section":"52.212-3.h.2"},{"answerText":"No","section":"52.212-3.h.4"},{"answerText":"No","section":"52.212-3.h.3"},{"answerText":"No","section":"52.212-3.h.5"},{"answerText":"No","section":"52.212-3.i.2.i"},{"section":"52.212-3.j"},{"answerText":"Vendor will provide information with specific offers to the Government","section":"52.212-3.k.1.2.i"},{"answerText":"Vendor will provide information with specific offers to the Government","section":"52.212-3.k.2.2"},{"answerText":"Corporate Entity (Not Tax Exempt)","section":"52.212-3.l.4"},{"answerText":"No","section":"52.212-3.l.5"},{"answerText":"No","section":"52.212-3.n.2.i"},{"answerText":"No","section":"52.212-3.n.2.ii"},{"answerText":"No","section":"52.212-3.p.1"},{"answerText":"No","section":"52.212-3.r.1"},{"answerText":"No","section":"52.226-2.b.1"},{"answerText":"No","section":"52.226-2.b.2"}],"id":"FAR 52.212-3"},{"answers":[{"section":"52.219-1.b","naics":[{"naicsName":"Other Computer Related Services","hasSizeChanged":"null","isPrimary":false,"ExcpCounter":1,"isSmallBusiness":"N","naicsCode":541519},{"naicsName":"Information Technology Value Added Resellers","hasSizeChanged":"null","isPrimary":false,"ExcpCounter":2,"isSmallBusiness":"N","naicsCode":541519},{"naicsName":"Engineering Services","hasSizeChanged":"null","isPrimary":false,"ExcpCounter":1,"isSmallBusiness":"N","naicsCode":541330},{"naicsName":"Military and Aerospace Equipment and Military Weapons","hasSizeChanged":"null","isPrimary":false,"ExcpCounter":2,"isSmallBusiness":"N","naicsCode":541330},{"naicsName":"Contracts and Subcontracts for Engineering Services Awarded Under the National Energy Policy Act of 1992","hasSizeChanged":"null","isPrimary":false,"ExcpCounter":3,"isSmallBusiness":"N","naicsCode":541330},{"naicsName":"Marine Engineering and Naval Architecture","hasSizeChanged":"null","isPrimary":false,"ExcpCounter":4,"isSmallBusiness":"N","naicsCode":541330},{"naicsName":"Software Publishers","hasSizeChanged":"null","isPrimary":true,"isSmallBusiness":"N","naicsCode":511210},{"naicsName":"Wired Telecommunications Carriers","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":517311},{"naicsName":"Data Processing, Hosting, and Related Services","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":518210},{"naicsName":"Custom Computer Programming Services","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":541511},{"naicsName":"Computer Systems Design Services","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":541512},{"naicsName":"Computer Facilities Management Services","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":541513},{"naicsName":"Other Scientific and Technical Consulting Services","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":541690},{"naicsName":"All Other Professional, Scientific, and Technical Services","hasSizeChanged":"null","isPrimary":false,"isSmallBusiness":"N","naicsCode":541990}]},{"answerText":"No","section":"52.219-1.b.8.ii"}],"id":"FAR 52.219-1"},{"answers":{"answerText":"Vendor will provide information with specific offers to the Government","section":"52.227-15.b.2"},"id":"FAR 52.227-15"},{"answers":{"answerText":"No","section":"52.204-17.b"},"id":"FAR 52.204-17"},{"answers":{"answerText":"No","section":"FAR 52.204-20.b"},"id":"FAR 52.204-20"},{"answers":{"answerText":"No","section":"52.222-18.c.1"},"id":"FAR 52.222-18"},{"answers":[{"answerText":"Yes","section":"52.222-22.a"},{"answerText":"Yes","section":"52.222-22.b"}],"id":"FAR 52.222-22"},{"answers":{"answerText":"Microsoft Corporation  has developed and has on file affirmative action programs required by Secretary of Labor regulations.","section":"52.222-25"},"id":"FAR 52.222-25"},{"answers":[{"answerText":"No","section":"52.209-11.b.1"},{"answerText":"No","section":"52.209-11.b.2"}],"id":"FAR 52.209-11"},{"answers":{"answerText":"Vendor will provide information with specific offers to the Government","section":"52.225-2.a"},"id":"FAR 52.225-2"},{"answers":{"answerText":"Vendor will provide information with specific offers to the Government","section":"52.225-4.a"},"id":"FAR 52.225-4"},{"answers":{"answerText":"Vendor will provide information with specific offers to the Government","section":"52.225-6.a"},"id":"FAR 52.225-6"},{"answers":{"answerText":"Vendor will provide information with specific offers to the Government","section":"52.222-48.a.1"},"id":"FAR 52.222-48"},{"answers":{"answerText":"Vendor will provide information with specific offers to the Government","section":"52.222-52.a.1.1"},"id":"FAR 52.222-52"},{"answers":{"answerText":"No","section":"SF330.1"},"id":"SF330"}],"dfarResponses":[{"answers":{"answerText":"No","section":"252.225-7022.b"},"id":"DFAR 252.225-7022"},{"answers":{"answerText":"No","section":"DFAR 252.216-7008.a"},"id":"DFAR 252.216-7008"},{"answers":{"answerText":"No","section":"DFAR 252.209-7002.1"},"id":"DFAR 252.209-7002"},{"answers":{"answerText":"Vendor will provide information with specific offers to the Government","section":"252.225-7000.c.1"},"id":"DFAR 252.225-7000"},{"answers":{"answerText":"Vendor will provide information with specific offers to the Government","section":"252.225-7020.c.1"},"id":"DFAR 252.225-7020"},{"answers":{"answerText":"Vendor will provide information with specific offers to the Government","section":"252.225-7022.c.1"},"id":"DFAR 252.225-7022"},{"answers":{"answerText":"Vendor will provide information with specific offers to the Government","section":"252.225-7035.c.1"},"id":"DFAR 252.225-7035"},{"answers":[{"answerText":"No","section":"252.225-7049.c.1"},{"answerText":"No","section":"252.225-7049.c.2"},{"answerText":"No","section":"252.225-7049.c.3"},{"answerText":"No","section":"252.225-7049.c.4"}],"id":"DFAR 252.225-7049"}]},"corporateStructureName":"Corporate Entity (Not Tax Exempt)","statusMessage":"Active","stateOfIncorporation":"WA","disasterRelief":"null","doingBusinessAsName":"Microsoft","mailingAddress":{"zip":"98052","city":"REDMOND","countryCode":"USA","stateorProvince":"WA","line2":"null","line1":"ONE MICROSOFT WAY","zipPlus4":"null"},"hasKnownExclusion":false,"altPastPerformancePoc":{"nonUsPhone":"null","firstName":"MELISSA","lastName":"RANSLEM","middleInitial":"null","address":{"zip":"20190","city":"RESTON","countryCode":"USA","stateorProvince":"VA","line2":"null","line1":"12012 SUNSET HILLS ROAD","zipPlus4":"null"},"notes":"null","usPhoneExt":"null","title":"CONTRACTS MANAGER","fax":"4257086748","usPhone":"5713532701","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="da979f96889a979399889589959c8ef4999597" rel="noreferrer noopener nofollow">[email protected]</a>"},"legalBusinessName":"Microsoft Corporation","duns":"081466849","divisionNumber":"null","activationDate":"2019-10-02 11:00:10.02812","naics":[{"naicsName":"Custom Computer Programming Services","isPrimary":false,"naicsCode":"541511"},{"naicsName":"Other Scientific and Technical Consulting Services","isPrimary":false,"naicsCode":"541690"},{"naicsName":"Computer Systems Design Services","isPrimary":false,"naicsCode":"541512"},{"naicsName":"Data Processing, Hosting, and Related Services","isPrimary":false,"naicsCode":"518210"},{"naicsName":"Other Computer Related Services","isPrimary":false,"naicsCode":"541519"},{"naicsName":"Computer Facilities Management Services","isPrimary":false,"naicsCode":"541513"},{"naicsName":"Software Publishers","isPrimary":true,"naicsCode":"511210"},{"naicsName":"All Other Professional, Scientific, and Technical Services","isPrimary":false,"naicsCode":"541990"},{"naicsName":"Engineering Services","isPrimary":false,"naicsCode":"541330"},{"naicsName":"Wired Telecommunications Carriers","isPrimary":false,"naicsCode":"517311"}],"status":"ACTIVE"}}}

最佳答案

您需要一个自定义转换器。

您正在尝试转换复杂类型,因此 Newtonsoft 不知道该怎么做。该修复相当简单,记录如下:Using a Custom JsonConverter to fix bad JSON results .

由于链接变坏,您需要的是以下内容:

class SingleValueArrayConverter<T> : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        object retVal = new Object();
        if (reader.TokenType == JsonToken.StartObject)
        {
            T instance = (T)serializer.Deserialize(reader, typeof(T));
            retVal = new [] { instance };
        }
        else if (reader.TokenType == JsonToken.StartArray)
        {
            retVal = serializer.Deserialize(reader, objectType);
        }
        return retVal;
    }

    public override bool CanConvert(Type objectType) => objectType == typeof(T);
}

然后,您可以创建一些对象来表示您的数据:

class SamPointOfContact
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string title { get; set; }
}

class Naics
{
    public string naicsName { get; set; }
    public string hasSizeChanged { get; set; }
    public bool isPrimary { get; set; }
    public int ExcpCounter { get; set; }
    public string isSmallBusiness { get; set; }
    public int naicsCode { get; set; }
}

class Answer
{
    public string answerText { get; set; }
    public string section { get; set; }
    public SamPointOfContact samPointOfContact { get; set; }
    public Naics[] naics { get; set; }
}

class AnswerNode
{
    [JsonConverter(typeof(SingleValueArrayConverter<Answer>))]
    public Answer[] answers { get; set; }
    public string id { get; set; }
}

class Root
{
    public AnswerNode[] listOfAnswers { get; set; }
}

此外,我使用了数组而不是List,而链接的博客则使用了List。如果您愿意,可以更改。

最后,像平常一样使用 Newtonsoft 反序列化:

Root result =
    JsonConvert.DeserializeObject<Root>(
        System.IO.File.ReadAllText("test.json"));

现在,我通过获取你的 JSON 并将其放入一个快速数组中来完成此操作 { "listOfAnswers": [ ... 你的原始 JSON ... ] },您需要修改它以适合您的实际数据。

关于c# - 使用不同类型的节点从 JSON 创建类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58454504/

相关文章:

ios - 我应该使用 NSCoder 序列化为 JSON 吗?

c# - ISerializable - 序列化单例

javascript - 一个棘手的javascript问题

c# - 在 XNA 中制作游戏窗口 "always on top"

json - Play Framework : How to sort JSON alphabetically

json - 我们如何通过网络安全地将 API key 和 secret token 交付给用户?

php - 根据 JSON 数据创建包含左侧和顶部标题的表格

c# - 标签上的 Caliburn Micro 运行代码点击

c# - 如何在 Razor View 中检查模型字符串属性是否为 null

c# - WP8后退按钮后退两步