简体   繁体   English

将Json对象反序列化为C#

[英]deserializing Json Object to C#

Trying to deserialize json but as it has dynamic key value , unable to get the the data in the testTimedict object 尝试反序列化json但由于它具有动态键值,无法获取testTimedict对象中的数据

JSON: JSON:

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

C#: 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; }
    }
}

I am deserializing the string into dictionary but the deserializer returns nothing for the inner most child object ie "123456"{} 我将字符串反序列化为字典,但反序列化器为最内部的子对象返回任何内容,即“123456”{}

I use your classes but please consider better naming for them 我使用你的课程,但请考虑更好的命名

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

and you can deserialize with Json.Net - JsonConvert.DeserializeObject<testroot>(test); 并且您可以使用Json.Net反序列化 - JsonConvert.DeserializeObject<testroot>(test);

Your "dynamic keys" for the timesheets are the same as the id properties inside your timesheet objects, so why not get rid of the duplication and make timesheets an array? 时间表的“动态键”与时间表对象中的id属性相同,那么为什么不摆脱重复并使timesheets成为一个数组呢? Like so: 像这样:

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

That cleans up your c# model considerably: 这大大清理了你的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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM