Solved

reading json from C# REST service

Posted on 2016-10-04
1
76 Views
Last Modified: 2016-10-05
Hi, I have recently created a REST service using C# console and OWIN.    It works fine but I wan't to take it a step further and I don't know how I can achieve my goal.    

Below is my code, as you can see I'm using ApiController to read the json data received.    What I would achieve is to read the json variable "type" to know what I have received and then depending if it's a customer, product or else I would like to read the variable "content" knowing what to expect so I can cast "content" as a Customer object for instance.

The actual json content I would like to send is this, I can read the "type" field but I don't know how to get "content" and send the values to the "customer" object I have.   I'm pretty sure it can be done, it's a matter of getting the POST parameter, analyse it and read it as a json content.    Currently I can only achieve reading json as a content automatically when receiving a POST command.

    {
        "type": "customer",
        "content": {          
          "Name": "test",
          "Adress": "test adress",
          }
    }

Here is the APIController:

    public class MyApiController : ApiController
    {
        public void Post(JsonStruct json)     // [FromBody]
        {
        if (json.type.Equals("customer", StringComparison.OrdinalIgnoreCase))
             {
                    // read content as a Customer class
        }
       }
    }      

Here is the expected fields:
    public class JsonStruct
    {
        public string type { get; set; }
        public Stream content { get; set; }
    }

Here is an example of Customer record I would like to build:
    public class Customer
    {
        public string name { get; set; }
        public string adress { get; set; }
    }
0
Comment
Question by:Buropro-Citation
1 Comment
 
LVL 42

Accepted Solution

by:
zephyr_hex (Megan) earned 500 total points
ID: 41828879
The way I've handled this in the past is to create a class that mimics the structure of the JSON.  There's actually an online tool that will help you construct the class based on the JSON:

http://json2csharp.com/


You can then deserialize using JSON.NET
1

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now