猿问

Json字符串转换为C#对象-数组转换为JsonString

我正在C#中收到JSON字符串,如下所示:


{

    "Id": "617723",

    "Acronym": "",

    "FirstName": "XXXXX",

    "LastName": "XXXXX",

    "Groupe": {

        "Code": "XXXXX",

        "Traductions": {

            "French": "",

            "English": "XXXXX"

        }

    },

    "BusinessUnit": {

        "Code": "XXXXX",

        "Traductions": {

            "French": "",

            "English": "XXXXX"

        }

    },

    "Team": {

        "Code": null,

        "Traductions": {

            "French": "",

            "English": null

        }

    },

    "Title": {

        "Code": null,

        "Traductions": {

            "French": "",

            "English": "XXXXX"

        }

    },

    "Title2": {

        "Code": null,

        "Traductions": {

            "French": "",

            "English": null

        }

    },

    "JobCategory": {

        "Code": "XXXXX",

        "Traductions": {

            "French": "",

            "English": "XXXXX"

        }

    },

    "PhoneList": [],

    "DateHired": "XXXXX",

    "DateTerminated": "XXXXX",

    "Gender": "XXXXX",

    "ManagerId": "XXXXX",

    "ManagerAcronym": "XXXXX",

    "IsManager": false,

    "Email": null,

    "CarLicense": null,

    "MyTeam": [],

    "HomeBase": {

        "Code": "XXXXX",

        "Traductions": {

            "French": "XXXXX",

            "English": "XXXXX"

        }

    },

    "Country": {

        "Code": "XXXXX",

        "Traductions": {

            "French": "XXXXXX",

            "English": "XXXXX"

        }

    },

    "State": {

        "Code": "XXXXX",

        "Traductions": {

            "French": "XXXXX",

            "English": "XXXXX"

        }

    },


当我尝试将其反序列化为时List<UsersJson>,出现以下错误:

无法将当前JSON对象(例如{\“ name \”:\“ value \”})反序列化为类型'System.String []',因为该类型需要JSON数组(例如[1,2,3])来反序列化正确地。要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型。数组或列表),可以从JSON对象反序列化。还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。路径'[0] .Groupe.Code',第1行,位置87。”

因此,Group如果不是,我应该如何声明该属性string[]


白衣染霜花
浏览 416回答 3
3回答

MM们

使用正确的类。我按照这里概述的步骤进行了重构:RootObject r = JsonConvert.DeserializeObject<RootObject>(json);public class Traductions{&nbsp; &nbsp; public string French { get; set; }&nbsp; &nbsp; public string English { get; set; }}public class Groupe{&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public Traductions Traductions { get; set; }}public class BusinessUnit{&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public Traductions Traductions { get; set; }}public class Team{&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public Traductions Traductions { get; set; }}public class Title{&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public Traductions Traductions { get; set; }}public class JobCategory{&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public Traductions Traductions { get; set; }}public class HomeBase{&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public Traductions Traductions { get; set; }}public class Country{&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public Traductions Traductions { get; set; }}public class State{&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public Traductions Traductions { get; set; }}public class City{&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public Traductions Traductions { get; set; }}public class ProfessionalTitle{&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public Traductions Traductions { get; set; }}public class RootObject{&nbsp; &nbsp; public string Id { get; set; }&nbsp; &nbsp; public string Acronym { get; set; }&nbsp; &nbsp; public string FirstName { get; set; }&nbsp; &nbsp; public string LastName { get; set; }&nbsp; &nbsp; public Groupe Groupe { get; set; }&nbsp; &nbsp; public BusinessUnit BusinessUnit { get; set; }&nbsp; &nbsp; public Team Team { get; set; }&nbsp; &nbsp; public Title Title { get; set; }&nbsp; &nbsp; public Title Title2 { get; set; }&nbsp; &nbsp; public JobCategory JobCategory { get; set; }&nbsp; &nbsp; public List<object> PhoneList { get; set; }&nbsp; &nbsp; public string DateHired { get; set; }&nbsp; &nbsp; public string DateTerminated { get; set; }&nbsp; &nbsp; public string Gender { get; set; }&nbsp; &nbsp; public string ManagerId { get; set; }&nbsp; &nbsp; public string ManagerAcronym { get; set; }&nbsp; &nbsp; public bool IsManager { get; set; }&nbsp; &nbsp; public string Email { get; set; }&nbsp; &nbsp; public string CarLicense { get; set; }&nbsp; &nbsp; public List<object> MyTeam { get; set; }&nbsp; &nbsp; public HomeBase HomeBase { get; set; }&nbsp; &nbsp; public Country Country { get; set; }&nbsp; &nbsp; public State State { get; set; }&nbsp; &nbsp; public City City { get; set; }&nbsp; &nbsp; public string ShirtSize { get; set; }&nbsp; &nbsp; public string LanguageAddressBook { get; set; }&nbsp; &nbsp; public string LanguagePrefered { get; set; }&nbsp; &nbsp; public string Local { get; set; }&nbsp; &nbsp; public string Mailbox { get; set; }&nbsp; &nbsp; public string HomeBusinessUnit { get; set; }&nbsp; &nbsp; public string JobType { get; set; }&nbsp; &nbsp; public string UnionCode { get; set; }&nbsp; &nbsp; public ProfessionalTitle ProfessionalTitle { get; set; }&nbsp; &nbsp; public bool IconEmailActif { get; set; }&nbsp; &nbsp; public bool IconSkypeActif { get; set; }}

肥皂起泡泡

可接受的答案有效,但是我觉得它有很多重复代码。您只需要另外两个模型类即可工作:public class Traductions{&nbsp; &nbsp; public string French { get; set; }&nbsp; &nbsp; public string English { get; set; }}public class CodeTraduction{&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public Traductions Traductions { get; set; }}public class RootObject{&nbsp; &nbsp; public string Id { get; set; }&nbsp; &nbsp; public string Acronym { get; set; }&nbsp; &nbsp; public string FirstName { get; set; }&nbsp; &nbsp; public string LastName { get; set; }&nbsp; &nbsp; public CodeTraduction Groupe { get; set; }&nbsp; &nbsp; public CodeTraduction BusinessUnit { get; set; }&nbsp; &nbsp; public CodeTraduction Team { get; set; }&nbsp; &nbsp; public CodeTraduction Title { get; set; }&nbsp; &nbsp; public CodeTraduction Title2 { get; set; }&nbsp; &nbsp; public CodeTraduction JobCategory { get; set; }&nbsp; &nbsp; public List<object> PhoneList { get; set; }&nbsp; &nbsp; public string DateHired { get; set; }&nbsp; &nbsp; public string DateTerminated { get; set; }&nbsp; &nbsp; public string Gender { get; set; }&nbsp; &nbsp; public string ManagerId { get; set; }&nbsp; &nbsp; public string ManagerAcronym { get; set; }&nbsp; &nbsp; public bool IsManager { get; set; }&nbsp; &nbsp; public string Email { get; set; }&nbsp; &nbsp; public string CarLicense { get; set; }&nbsp; &nbsp; public List<object> MyTeam { get; set; }&nbsp; &nbsp; public CodeTraduction HomeBase { get; set; }&nbsp; &nbsp; public CodeTraduction Country { get; set; }&nbsp; &nbsp; public CodeTraduction State { get; set; }&nbsp; &nbsp; public CodeTraduction City { get; set; }&nbsp; &nbsp; public string ShirtSize { get; set; }&nbsp; &nbsp; public string LanguageAddressBook { get; set; }&nbsp; &nbsp; public string LanguagePrefered { get; set; }&nbsp; &nbsp; public string Local { get; set; }&nbsp; &nbsp; public string Mailbox { get; set; }&nbsp; &nbsp; public string HomeBusinessUnit { get; set; }&nbsp; &nbsp; public string JobType { get; set; }&nbsp; &nbsp; public string UnionCode { get; set; }&nbsp; &nbsp; public CodeTraduction ProfessionalTitle { get; set; }&nbsp; &nbsp; public bool IconEmailActif { get; set; }&nbsp; &nbsp; public bool IconSkypeActif { get; set; }}使用示例JSON,您可以像这样反序列化它:var item = JsonConvert.DeserializeObject<RootObject>(json);您可以根据需要创建单独的类,但可以保留一个基本类CodeTraduction,例如:public class Groupe : CodeTraduction{&nbsp; &nbsp; //Add any "Groupe" specific properties here}

繁华开满天机

您将需要另外几个带有Code和Traductions属性的类。public class Groupe {&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public Traductions Traductions { get; set; }}public class Traductions {&nbsp; &nbsp; public string French { get; set; }&nbsp; &nbsp; public string English { get; set; }}用Groupe在string[]您的基类中,您应该是黄金的:)
随时随地看视频慕课网APP
我要回答