Solved

reading json from C# REST service

Posted on 2016-10-04
1
103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 43

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

756 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