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?

1
  • 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. Commented Feb 21, 2016 at 20:24

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
@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?
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
@AdamWeitzman Ah ok... I understand. You would typically use WebClient class. There are already answered questions on this topic: .NET: Simplest way to send POST with data and read response and HTTP request with post.
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){
}

Comments

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.

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.