Solved

reading json from C# REST service

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 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…

747 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

14 Experts available now in Live!

Get 1:1 Help Now