2

The TrackIt Web API (I can't change it) gives me this JSON inside a return object:

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

Where there are N number of notes. I can't figure out how to deserialize this using JSON.Net unless I do something that feels wrong like:

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

And in the parent:

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

This "works" by using :

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

I think there must be a better way to get the notes without pre-defining N number of them in the object. I believe TrackIt might have made this difficult by not putting the "Notes" in an array, but is there a smarter way to do this?

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • I ended up using an adaptation of this: http://stackoverflow.com/questions/17745814/deserialize-the-json-where-the-values-are-field-names-with-json-net?rq=1 – user3447172 Mar 21 '14 at 20:27

1 Answers1

1

Use a Dictionary<string, Note> for the notes:

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; }     
    ...
}
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300