Json字符串到C#Object - 数组到JsonString

时间:2018-04-16 13:40:43

标签: c# json

我在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"
        }
    },
    "City": {
        "Code": "XXXXX",
        "Traductions": {
            "French": "XXXXX",
            "English": "XXXXX"
        }
    },
    "ShirtSize": "",
    "LanguageAddressBook": "XXXXX",
    "LanguagePrefered": null,
    "Local": null,
    "Mailbox": null,
    "HomeBusinessUnit": "1",
    "JobType": "XXXXXX",
    "UnionCode": "",
    "ProfessionalTitle": {
        "Code": null,
        "Traductions": {
            "French": "",
            "English": null
        }
    },
    "IconEmailActif": true,
    "IconSkypeActif": true
}

我想在C#对象中转换它,所以我将我的模型设为:

public class UsersJson
{
    public string Acronym { get; set; }

    public string[] BusinessUnit { get; set; }

    public string CarLicense { get; set; }

    public string[] City { get; set; }

    public string[] Country { get; set; }

    public string DateHired { get; set; }

    public string DateTerminated { get; set; }

    public string Email { get; set; }

    public string FirstName { get; set; }

    public string Gender { get; set; }

    public string[] Groupe { get; set; }

    public string[] HomeBase { get; set; }

    public string HomeBusinessUnit { get; set; }

    public bool IconEmailActif { get; set; }

    public bool IconSkypeActif { get; set; }

    public string Id { get; set; }

    public bool IsManager { get; set; }

    public string[] JobCategory { get; set; }

    public string JobType { get; set; }

    public string LanguageAddressBook { get; set; }

    public string LanguagePrefered { get; set; }

    public string LastName { get; set; }

    public string Local { get; set; }

    public string Mailbox { get; set; }

    public string ManagerAcronym { get; set; }

    public string ManagerId { get; set; }

    public string[] MyTeam { get; set; }

    public string[] PhoneList { get; set; }

    public string[] ProfessionalTitle { get; set; }

    public string ShirtSize { get; set; }

    public string[] State { get; set; }

    public string[] Team { get; set; }

    public string[] Title { get; set; }

    public string[] Title2 { get; set; }

    public string UnionCode { get; set; }
}

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

  

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

那么,如果属性Group不是string[],我应该如何声明它?

4 个答案:

答案 0 :(得分:1)

使用正确的课程。我跟着steps outlined here并重构了一下:

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; }
}

答案 1 :(得分:0)

您需要使用CodeTraductions属性的其他几个课程。

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[],您应该是金色的:)

答案 2 :(得分:0)

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

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
}

答案 3 :(得分:0)

为了将来参考,您还可以使用“选择性粘贴”。您复制JSON文件( Ctrl + C )并添加新的类文件(如果您还没有)。然后你进入编辑(在VS的左上角)并将光标悬停在Paste Special上,你将得到两个选项,一个用于XML作为Class或JSON作为Class。 希望它能在未来帮助您和其他人。 :)