为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
白衣染霜花 2021-04-08 13:15:18
我正在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[]?
查看完整描述

3 回答

?
MM们

TA贡献1886条经验 获得超2个赞

使用正确的类。我按照这里概述的步骤进行了重构:


RootObject r = JsonConvert.DeserializeObject<RootObject>(json);


public class Traductions

{

    public string French { get; set; }

    public string English { get; set; }

}


public class Groupe

{

    public string Code { get; set; }

    public Traductions Traductions { get; set; }

}


public class BusinessUnit

{

    public string Code { get; set; }

    public Traductions Traductions { get; set; }

}


public class Team

{

    public string Code { get; set; }

    public Traductions Traductions { get; set; }

}


public class Title

{

    public string Code { get; set; }

    public Traductions Traductions { get; set; }

}


public class JobCategory

{

    public string Code { get; set; }

    public Traductions Traductions { get; set; }

}


public class HomeBase

{

    public string Code { get; set; }

    public Traductions Traductions { get; set; }

}


public class Country

{

    public string Code { get; set; }

    public Traductions Traductions { get; set; }

}


public class State

{

    public string Code { get; set; }

    public Traductions Traductions { get; set; }

}


public class City

{

    public string Code { get; set; }

    public Traductions Traductions { get; set; }

}


public class ProfessionalTitle

{

    public string Code { get; set; }

    public Traductions Traductions { get; set; }

}


public class RootObject

{

    public string Id { get; set; }

    public string Acronym { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public Groupe Groupe { get; set; }

    public BusinessUnit BusinessUnit { get; set; }

    public Team Team { get; set; }

    public Title Title { get; set; }

    public Title Title2 { get; set; }

    public JobCategory JobCategory { get; set; }

    public List<object> PhoneList { get; set; }

    public string DateHired { get; set; }

    public string DateTerminated { get; set; }

    public string Gender { get; set; }

    public string ManagerId { get; set; }

    public string ManagerAcronym { get; set; }

    public bool IsManager { get; set; }

    public string Email { get; set; }

    public string CarLicense { get; set; }

    public List<object> MyTeam { get; set; }

    public HomeBase HomeBase { get; set; }

    public Country Country { get; set; }

    public State State { get; set; }

    public City City { get; set; }

    public string ShirtSize { get; set; }

    public string LanguageAddressBook { get; set; }

    public string LanguagePrefered { get; set; }

    public string Local { get; set; }

    public string Mailbox { get; set; }

    public string HomeBusinessUnit { get; set; }

    public string JobType { get; set; }

    public string UnionCode { get; set; }

    public ProfessionalTitle ProfessionalTitle { get; set; }

    public bool IconEmailActif { get; set; }

    public bool IconSkypeActif { get; set; }

}



查看完整回答
反对 回复 2021-04-24
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

可接受的答案有效,但是我觉得它有很多重复代码。您只需要另外两个模型类即可工作:


public class Traductions

{

    public string French { get; set; }

    public string English { get; set; }

}


public class CodeTraduction

{

    public string Code { get; set; }

    public Traductions Traductions { get; set; }

}


public class RootObject

{

    public string Id { get; set; }

    public string Acronym { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public CodeTraduction Groupe { get; set; }

    public CodeTraduction BusinessUnit { get; set; }

    public CodeTraduction Team { get; set; }

    public CodeTraduction Title { get; set; }

    public CodeTraduction Title2 { get; set; }

    public CodeTraduction JobCategory { get; set; }

    public List<object> PhoneList { get; set; }

    public string DateHired { get; set; }

    public string DateTerminated { get; set; }

    public string Gender { get; set; }

    public string ManagerId { get; set; }

    public string ManagerAcronym { get; set; }

    public bool IsManager { get; set; }

    public string Email { get; set; }

    public string CarLicense { get; set; }

    public List<object> MyTeam { get; set; }

    public CodeTraduction HomeBase { get; set; }

    public CodeTraduction Country { get; set; }

    public CodeTraduction State { get; set; }

    public CodeTraduction City { get; set; }

    public string ShirtSize { get; set; }

    public string LanguageAddressBook { get; set; }

    public string LanguagePrefered { get; set; }

    public string Local { get; set; }

    public string Mailbox { get; set; }

    public string HomeBusinessUnit { get; set; }

    public string JobType { get; set; }

    public string UnionCode { get; set; }

    public CodeTraduction ProfessionalTitle { get; set; }

    public bool IconEmailActif { get; set; }

    public bool IconSkypeActif { get; set; }

}

使用示例JSON,您可以像这样反序列化它:


var item = JsonConvert.DeserializeObject<RootObject>(json);

您可以根据需要创建单独的类,但可以保留一个基本类CodeTraduction,例如:


public class Groupe : CodeTraduction

{

    //Add any "Groupe" specific properties here

}


查看完整回答
反对 回复 2021-04-24
?
繁华开满天机

TA贡献1816条经验 获得超4个赞

您将需要另外几个带有Code和Traductions属性的类。


public class Groupe {

    public string Code { get; set; }

    public Traductions Traductions { get; set; }

}


public class Traductions {

    public string French { get; set; }

    public string English { get; set; }

}

用Groupe在string[]您的基类中,您应该是黄金的:)


查看完整回答
反对 回复 2021-04-24
  • 3 回答
  • 0 关注
  • 415 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信