3 回答
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; }
}
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
}
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[]您的基类中,您应该是黄金的:)
- 3 回答
- 0 关注
- 415 浏览
添加回答
举报