将Json对象反序列化为C#

时间:2017-06-28 16:12:15

标签: c# json api parsing

尝试反序列化json但由于它具有动态键值,无法获取testTimedict对象中的数据

JSON:

{ 
  "results": {  
    "timesheets": {
      "123456": { 
        "id": 123456,
        "user_id": 36036,
        "jobcode_id": 374398,
        ..
      },
      "635241": {
        "id": 635241,
        "user_id": 36036,
        "jobcode_id": 0,
        ..
      },
      ..   
    }
  }
}

C#:

public class testroot
{
    private testresults _results;

    [DataMember(Name = "results")]
    public testresults Results {get; set;}
}

[DataContract()]
public class testresults
{
    private testTimedict _timesheet;

    [DataMember(Name = "timesheets")]
    public testTimedict timesheetList {get; set;}
}

[DataContract(Name = "timesheets")]
public class testTimedict
{
    private Dictionary<string, TimeSheets> _timesheet;

    [DataMember()]
    public Dictionary<string, TimeSheets> timesheetList {get; set;}
}

public class TimeSheets
{

    private int _id;
    private int _user_id;
    private int _jobcode_id;
    [DataMember(Name = "id")]
    public int ID {
        get { return _id; }
        set { _id = value; }
    }

    [DataMember(Name = "user_id")]
    public int user_id {
        get { return _user_id; }
        set { _user_id = value; }
    }

    [DataMember(Name = "jobcode_id")]
    public int jobcode_id {
        get { return _jobcode_id; }
        set { _jobcode_id = value; }
    }
}

我将字符串反序列化为字典,但反序列化器不会为最内层的子对象返回任何内容,即&#34; 123456&#34; {}

2 个答案:

答案 0 :(得分:0)

我使用你的课程,但请考虑为他们提供更好的命名

[DataContract]
public class testroot
{
    private testTimedict _results;

    [DataMember(Name = "results")]
    public testTimedict Results { get; set; }
}

[DataContract]
public class testTimedict
{
    private Dictionary<string, TimeSheets> _timesheet;

    [DataMember(Name = "timesheets")]
    public Dictionary<string, TimeSheets> timesheetList { get; set; }
}

[DataContract]
public class TimeSheets
{

    private int _id;
    private int _user_id;
    private int _jobcode_id;
    [DataMember(Name = "id")]
    public int ID
    {
        get { return _id; }
        set { _id = value; }
    }

    [DataMember(Name = "user_id")]
    public int user_id
    {
        get { return _user_id; }
        set { _user_id = value; }
    }

    [DataMember(Name = "jobcode_id")]
    public int jobcode_id
    {
        get { return _jobcode_id; }
        set { _jobcode_id = value; }
    }
}

您可以使用Json.Net反序列化 - JsonConvert.DeserializeObject<testroot>(test);

答案 1 :(得分:0)

您的&#34;动态键&#34;对于时间表与时间表对象中的id属性相同,那么为什么不摆脱重复并使timesheets成为一个数组?像这样:

{ 
    "results": {  
        "timesheets": [ { 
            "id": 123456,
            "user_id": 36036,
            "jobcode_id": 374398
        }, {
            "id": 635241,
            "user_id": 36036,
            "jobcode_id": 0
        }]  
    }
}

这大大清理了你的c#模型:

public Results results { get; set; }

public class Timesheet
{
    public int id { get; set; }
    public int user_id { get; set; }
    public int jobcode_id { get; set; }
}

public class Results
{
    public List<Timesheet> timesheets { get; set; }
}