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?

1

1 Answer 1

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; }     
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.