1

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; }
}
Computer
  • 2,149
  • 7
  • 34
  • 71
  • 1
    I don't know what you have read but `JsonConvert` is a static class in Newtonsoft.Json. From there, you can call methods like `SerializeObject` etc. – bolkay Nov 22 '19 at 05:56
  • Very confusing how you make a single function to be opposite of the library that implements this function... I'm not even sure how to edit this question so it is neither "how to parse JSON" nor "what's your favorite way of parsing JSON"... – Alexei Levenkov Nov 22 '19 at 07:35
  • @AlexeiLevenkov seen to many examples most likely out of date, renamed etc – Computer Nov 22 '19 at 09:25

1 Answers1

2

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.

Alvin Quezon
  • 1,089
  • 3
  • 11
  • 29
  • You make that look pretty simple. Did you generate the classes manually or use an online converter by passing in some sample JSON? So you installed Newtonsoft and then utilised the JsonConvert class? – Computer Nov 22 '19 at 06:19
  • 1
    @Computer, yes I did create the class manually, And yes I install `Newtonsoft` and utilize the static class `JsonConvert` to do the job. – Alvin Quezon Nov 22 '19 at 06:26
  • 1
    @Computer, in addition, you can serialize any object without creating a object, the `Json.SerializeObject` will do the serializing base on your object you pass into the static method. The **sample object** i mentioned in answer section is for deserializing the data out of the json file. – Alvin Quezon Nov 22 '19 at 06:33
  • I decided to get your sample JSON, went to json2csharp.com converted it to a class and the result is under edit 1 in my original question. So the converter doesn't add the attributes (JSON Property). Should I add those manually? – Computer Nov 22 '19 at 06:46
  • 1
    @Computer, if your going to serialize it then no need for you to add the attributes since it is used only for deserializing the object. you can go ahead and skip the attribute `[JsonProperty('')]`. – Alvin Quezon Nov 22 '19 at 06:49
  • Considering I only want to get the JSON data from a 3rd party service and don't intend to send any data to them, I only need to serialise into objects so I can map these properties with the correct data field. In which case I don't need the attribute. However if in future I need to do this, do I surround all the properties myself with the attribute? Or is there a service that would generate the complete class? – Computer Nov 22 '19 at 07:05
  • 1
    @Computer, surely you can serialize object when you set the generic property `` to `dynamic` and will automatically create a dynamic object out of json data. So, in order to access the json object you have to pass a attribute name into the dynamic object example: `jsonData["itemId"]`. Unfortunately, in order for you to map those properties into an object you have to create a class and set the `JsonProperty` attribute; – Alvin Quezon Nov 22 '19 at 08:43