Solved

reading json from C# REST service

Posted on 2016-10-04
1
85 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# bracket error 3 30
Html5 Index on a table 7 25
show form submit button based on jquery sum 5 26
If condition on Html with Asp 11 13
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

785 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