Search code examples
c#asp.netasp.net-mvchttpwebrequest

JSON Net Vs JsonConvert


I'm looking to utilise a 3rd party service to authenticate, retrieve data and display it on a page.

The data would be returned in JSON format and will use HttpWebRequest to make the call.

I have got all the JSON URLs that I will use and converted them to C# classes using an online converter.

I am now trying to find a serialiser/deserialiser to convert the data into C# objects so I can hook the control with the data retrieved.

After some research, I'm confused if I should go with JsonConvert or Newtonsoft? Some have decided to create their own but I'm only repeating the wheel going down this road.

There's quite a number of articles but I rather invest some time in a more supported tool/version.

Does anyone know what/which serialiser and deserialiser I could look into for the task above?

I won't be using MVC but Asp webforms so not sure if that makes a difference. Would appreciate any examples of the tool to show how it would convert the data either way?

Edit 1

Result of sample data from answer converted to C# class

public class RootObject
 {
    public int itemId { get; set; }
    public string itemName { get; set; }
}

Solution

  • I always use Newtonsoft.Json library for mapping json data to an object, I personally use JsonConvert static class since it is easier to implement, here's how I do when mapping the json to object:

    Sample Json:

    [
      {
         "itemId": 1
         "itemName": "Item 1"
      },
      {
         "itemId": 2
         "itemName": "Item 2"
      },
      .
      .
      .
    ]
    

    Sample Object:

    public class ItemData
    {
       [JsonProperty("itemId")]
       public string ItemId { get; set; }
       [JsonProperty("itemName")]
       public string ItemName { get; set; }
    }
    

    Json convert:

    var serializeItem = JsonConvert.SerializeObject(yourJsonObjectHere); // serialize object
    var deserializeItem = JsonConvert.DeserializeObject<List<ItemData>>(yourJsonHereObject); // deserialize object
    

    It is base on your personal preference and I think (IMHO) that JsonConvert is much easier to use.