1

I'm coming from a node.js javascript everywhere upbringing. Learning .NET and struggling mightily with the strongly typed aspect of it. What is the quickest way to convert a large JSON object:

var body = new {
                   "stay": {
                       "checkIn": "2016-06-08",
                       "checkOut": "2016-06-10",
                       "shiftDays": "2"
                   },
                   "occupancies": [
                                      {
                                          "rooms": 1,
                                          "adults": 2,
                                          "children": 1,
                                          "paxes": [
                                                       {
                                                           "type": "AD",
                                                           "age": 30
                                                       },
                                                       {
                                                           "type": "AD",
                                                           "age": 30
                                                       },
                                                       {
                                                           "type": "CH",
                                                           "age": 8
                                                       }
                                                   ]
                                       }
                                   ],
                   "geolocation": {
                                      "longitude": 2.646633999999949,
                                      "latitude": 39.57119,
                                      "radius": 20,
                                      "unit": "km"
                                  }
               };

Into something that can be read in Visual Studio?

Markus Safar
  • 6,324
  • 5
  • 28
  • 44
Adam Weitzman
  • 1,552
  • 6
  • 23
  • 45
  • Hint: If you want a strongly typed class, VS has a paste option for JSON to create classes for you. Then just use Newtonsoft JSON or others to deserialize. – Sami Kuhmonen Feb 21 '16 at 20:24

3 Answers3

4

Acutally there are some possibilities but I'll point out 3 for you:

  1. You can create the same class hierarchy as your json object represents and deserialize your json into an instance of this created class.
  2. You can Visual Studio take care of creating these classes by copying your json and using Edit > Paste Special > Paste JSON as Classes.
  3. You can use the datatype dynamic and just deserialize into this datatype. The code would look like this: dynamic json = JsonConvert.DeserializeObject(yourJsonAsText);. If you do so you can programatically access everything within the deserialized instance but...
    • you will not have intellisense (because there is no class existing)
    • could maybe access something that "isnt there"

However you will need to install the Netwonsoft.Json package for the above 3 solutions. If you need a way without a 3rd party component/package, you can take a look at the following answers:

They show some ways provided by the .NET framework itself.

Community
  • 1
  • 1
Markus Safar
  • 6,324
  • 5
  • 28
  • 44
  • ok nice, once I've created the classes...do i send the post request with the classes as the body? I'm confused on how this would work – Adam Weitzman Feb 21 '16 at 20:31
  • @AdamWeitzman I'm not sure I understand exactly what you mean. If you have a json whether received via the network, loaded from a file or entered by the user itself (it doesn't matter how you receive it), you can use the above methods to deserialize the json (which is "text" at this moment) into an instance. Can you elaborate what you actually trying to build please? – Markus Safar Feb 21 '16 at 20:34
  • ok gotcha, so the best way i can explain this is in node terms...its a simple API post request, where I'm sending JSON to get a response. I'm so new to C# and .NET I can't understand what classes have to do with sending a POST request – Adam Weitzman Feb 21 '16 at 20:42
  • @AdamWeitzman Ah ok... I understand. You would typically use [`WebClient`](https://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.110%29.aspx) class. There are already answered questions on this topic: [.NET: Simplest way to send POST with data and read response](http://stackoverflow.com/questions/4088625/net-simplest-way-to-send-post-with-data-and-read-response) and [HTTP request with post](http://stackoverflow.com/questions/4015324/http-request-with-post). – Markus Safar Feb 22 '16 at 13:27
0

Install https://www.nuget.org/packages/Newtonsoft.Json/ if you are not using it yet then

JsonConvert.DeserializeObject<YourType>(string)

Other option, if you use asp.net, you can do is to create WebApi or MVc action and input parameter would be type you Send and .net will deserialize it for you

public void ActionName(YourType type){
}
Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80
0

You could try a library that will convert data for you like Jayrock or Json.NET

https://atifaziz.github.io/projects/jayrock/ or http://www.newtonsoft.com/json

These will convert your object into an object that doesn't have a specific type.