Results 1 to 5 of 5

Thread: Deserialize json into an object

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Deserialize json into an object

    I am getting json back from an api call to asana. I would think somewhere on this internet I could find definitions of asana objects. Otherwise, I would define my own, right? How do I take a string such as this:
    Code:
    {
      "data" : [ {
        "id" : 15-char-id,
        "created_at" : "2018-05-24T18:25:05.747Z",
        "created_by" : {
          "id" : 15-char-id,
          "name" : "MMOCK"
        },
        "text" : "added to Customer Pre-Sale Project Summary",
        "type" : "system"
      }, {
        "id" : 15-char-id,
        "created_at" : "2018-05-24T18:26:39.485Z",
        "created_by" : {
          "id" : 15-char-id,
          "name" : "MMOCK"
        },
        "text" : "added to Customer Implementation Scheduling",
        "type" : "system"
      }
    and deserialize it, like this person did with his Account object?

    Code:
    public class Account
    {
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public IList<string> Roles { get; set; }
    }
    string json = @"{
      'Email': 'james@example.com',
      'Active': true,
      'CreatedDate': '2013-01-20T00:00:00Z',
      'Roles': [
        'User',
        'Admin'
      ]
    }";
    
    Account account = JsonConvert.DeserializeObject<Account>(json);
    (I will write my objects if I have to. I'm just thinking someone will come along and say why are you doing that? It's right here on the internet.)
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Deserialize json into an object

    First things first, do not use the standard MS json converter, using NuGet get the NewtonSoft Json library.

    Everyone uses it as its much nicer and simpler to use and there is loads of documentation on it.

    here is example deserialize code form one of my apps -

    Code:
    TimesheetData result = JsonConvert.DeserializeObject<TimesheetData>(json);
    So JsonConvert is a NewtonSoftJson class, DeserializeObject the method, you give it the class type you want it to deserialize your json into in my case TimesheetData, then in the brackets you pass in your (json) and that it it does all the work for you and fill you list with objects based upon your json data.


    This is my TimesheetData class

    Code:
     public class TimesheetData 
        {
            [JsonProperty("Data")]
            public IList<Timesheet> Data
            {
                get;
                set;
            }
        }
    and my Timesheet class basically looks like this - (i have trimmed it down to just the first property)

    You will notice that this class features this tag -
    Code:
    [JsonProperty("UserCode")]
    this is NewtonSoftJson tag which just maps your property to the json property name.

    Code:
     public class Timesheet 
        {
            private int _UserCode;
    
    [JsonProperty("UserCode")]
            public int UserCode
            {
                get
                {
                    return _UserCode;
                }
                set
                {
                    Set(() => UserCode, ref _UserCode, value);
                }
            }
    }
    For your specific json example for the Created_By item you will need to create a class for as it has 2 properties,

    So you would have a main class with that standard properties of String, DateTime etc. but for the Created_By it will be type of your CreatedBy class which will need the 2 properties for id and name.

    thats it really give it a go and post back if you have any issues
    Last edited by NeedSomeAnswers; Jun 19th, 2018 at 08:33 AM.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: Deserialize json into an object

    Yes, I got it. It was much more trivial than I thought.

    Code:
        public class MyClassX
        {
            public List<RootObjectX> data { get; set; }
        }
        public class RootObjectX
        {
            public string id { get; set; }
            public string created_at { get; set; }
            public CreatedBy created_by { get; set; }
            public string text { get; set; }
            public string type { get; set; }
        }
        public class CreatedBy
        {
            public string id { get; set; }
            public string name { get; set; }
        }
    Now I have an asnana-specific question how to get only text that are truly user-entered notes and not just a note because someone assigned a task or added a follower. There doesn't seem to be a differentiation.

    P.S. I've got NewtonSoft Json library!
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Deserialize json into an object

    Yep json is easy and nice to use and quick too, its so much faster than XML which you will notice if you ever have to parse larger data sets

    not sure i can help with your Asana question, but will it not have a different type if its a user entered note ? or is the type system for those too?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  5. #5

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,478

    Re: Deserialize json into an object

    Quote Originally Posted by NeedSomeAnswers View Post
    will it not have a different type if its a user entered note ? or is the type system for those too?
    Yes! It is type "comment". LOL, thanks!!!
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width