JSON .net数组外的多个对象

时间:2014-03-21 16:12:37

标签: c# .net json json.net

TrackIt Web API(我无法改变它)在返回对象中提供了这个JSON:

...
Notes: {
    Note1: {
         CreatedBy: "smarteam2"
         ...
    },
    Note2: {
         CreatedBy: "smarteam2"
         CreatedDate: "1/27/2014 2:37:36 PM" 
         ...
    }, ...

哪里有N个音符。我无法弄清楚如何使用JSON.Net对其进行反序列化,除非我做了一些感觉错误的事情:

public class TrackItWorkOrderNotes
{
    public TrackItWorkOrderNotes Note1 { get; set; }
    public TrackItWorkOrderNotes Note2 { get; set; }
    public string IsPrivate { get; set; }
    public string FullText { get; set; }        
    public string WorkOrderNoteTypeId { get; set; }
}

在父母身上:

public class TrackitWorkOrder
{
    public TrackItWorkOrderNotes Notes { get; set; }
    public int Id { get; set; }
    ...

这"工作"使用:

ti = JsonConvert.DeserializeObject<TrackItResponse<TrackitWorkOrder>>(responseString);

我认为必须有更好的方法来获取笔记,而无需在对象中预先定义N个。我相信TrackIt可能因为没有把#34; Notes&#34;在数组中,但有更聪明的方法吗?

1 个答案:

答案 0 :(得分:1)

使用Dictionary<string, Note>注释:

public class TrackitWorkOrder
{
    public Dictionary<string, TrackItWorkOrderNote> Notes { get; set; }
    public int Id { get; set; }
    ...
}

public class TrackItWorkOrderNote
{
    public string CreatedBy { get; set; }
    public string CreatedDate { get; set; }     
    ...
}