locked
How to consume JSON with header in REST API? RRS feed

  • Question

  • User-740209433 posted

    Hi, 

    I have a JSON definition with header and the details:

    Header Definition: 

    BatchName : string

    Period: string

    Detail definition:

    ID:  Int

    Category: string

    Amount: Float

    What method should I use to serialize or deserialize the detail and the header?

    Thanks in advance and your help would be greatly appreciate!

    Wednesday, June 8, 2016 1:58 PM

Answers

  • User-2057865890 posted

    Hi Wkpli,

    ASP.NET Web API makes it pretty easy to pass complex data in the form of JSON to the server. If you want to capture an object you can simply create a controller method that has an object parameter and Web API automatically handles decoding JSON into the object's structure for you. You could call your API from the client side using jQuery and Javascript or from the server side using HttpClient.

    public HttpResponseMessage PassObject(YourObject object)
    {
    }

    reference: pass json POST data to Web API method as object

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 9, 2016 7:18 AM
  • User753101303 posted

    Hi,

    Serialization/deserialization is handled for you by Web API :  http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    So in most if not all cases you'll read incoming objects already deserialized for you or you'll return objects which will be serialized for you. In your case you'll likely just have a single header object with a property being a list of Details objects.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 9, 2016 7:50 AM
  • User842257015 posted

    Hi Wkpil,

    I had used Web api for REST service, 

    Json

    {
    	"Header": {
    		"BatchName": "ABC",
    		"Period": "CBA"
    	},
    	"Detail": {
    		"ID": 100,
    		"Category": "Book",
    		"Amount": 123.55
    	}
    
    }

    Model Class 

    public class HeaderDetail
    {
       public Header Header { get; set; }
       public Detail Detail { get; set; }
    }
    public class Header
    {
       public string BatchName { get; set; }
       public string Period { get; set; }
    }
    
    public class Detail
    {
       public int ID { get; set; }
       public string Category { get; set; }
       public double Amount { get; set; }
    }
    
    
    

    Web API Controller

    IList<HeaderDetail> headerDetail;
    //Constructor
    public ValuesController()
    {
    	headerDetail = new List<HeaderDetail>();
    	headerDetail.Add(new HeaderDetail
    	{
    		Detail = new Detail { ID = 100, Amount = 123.55, Category = "Book" },
    		Header = new Header { BatchName = "ABC", Period = "CBA" }
    	});
    
    	headerDetail.Add(new HeaderDetail
    	{
    		Detail = new Detail { ID = 101, Amount = 123.55, Category = "Book" },
    		Header = new Header { BatchName = "ABC", Period = "CBA" }
    	});
    }
    
    public IEnumerable<HeaderDetail> Get()
    {            
         return hd.GetHeaderDetail().ToList();
    }
    
    // GET api/values/5
    public HeaderDetail Get(int id)
    {
       return hd.GetHeaderDetail().Where(x => x.Detail.ID.Equals(id)).FirstOrDefault();
    }
    
    // POST api/values
     public void Post([FromBody]HeaderDetail value)
    {
      // you can get all posted HeaderDetail data  in value variable.
    }

    You can send data from client to service as JSON format like below. 

    Content-Type: application/json

    {
      "Header": {
      "BatchName": "dfsdfsadf",
      "Period": "sdfsdfsdfsadf"
    },
      "Detail": {
      "ID": 102,
      "Category": "dsfsdfll",
      "Amount": 12120.340
    }
    }



    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 9, 2016 8:00 AM

All replies

  • User-2057865890 posted

    Hi Wkpli,

    ASP.NET Web API makes it pretty easy to pass complex data in the form of JSON to the server. If you want to capture an object you can simply create a controller method that has an object parameter and Web API automatically handles decoding JSON into the object's structure for you. You could call your API from the client side using jQuery and Javascript or from the server side using HttpClient.

    public HttpResponseMessage PassObject(YourObject object)
    {
    }

    reference: pass json POST data to Web API method as object

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 9, 2016 7:18 AM
  • User753101303 posted

    Hi,

    Serialization/deserialization is handled for you by Web API :  http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    So in most if not all cases you'll read incoming objects already deserialized for you or you'll return objects which will be serialized for you. In your case you'll likely just have a single header object with a property being a list of Details objects.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 9, 2016 7:50 AM
  • User842257015 posted

    Hi Wkpil,

    I had used Web api for REST service, 

    Json

    {
    	"Header": {
    		"BatchName": "ABC",
    		"Period": "CBA"
    	},
    	"Detail": {
    		"ID": 100,
    		"Category": "Book",
    		"Amount": 123.55
    	}
    
    }

    Model Class 

    public class HeaderDetail
    {
       public Header Header { get; set; }
       public Detail Detail { get; set; }
    }
    public class Header
    {
       public string BatchName { get; set; }
       public string Period { get; set; }
    }
    
    public class Detail
    {
       public int ID { get; set; }
       public string Category { get; set; }
       public double Amount { get; set; }
    }
    
    
    

    Web API Controller

    IList<HeaderDetail> headerDetail;
    //Constructor
    public ValuesController()
    {
    	headerDetail = new List<HeaderDetail>();
    	headerDetail.Add(new HeaderDetail
    	{
    		Detail = new Detail { ID = 100, Amount = 123.55, Category = "Book" },
    		Header = new Header { BatchName = "ABC", Period = "CBA" }
    	});
    
    	headerDetail.Add(new HeaderDetail
    	{
    		Detail = new Detail { ID = 101, Amount = 123.55, Category = "Book" },
    		Header = new Header { BatchName = "ABC", Period = "CBA" }
    	});
    }
    
    public IEnumerable<HeaderDetail> Get()
    {            
         return hd.GetHeaderDetail().ToList();
    }
    
    // GET api/values/5
    public HeaderDetail Get(int id)
    {
       return hd.GetHeaderDetail().Where(x => x.Detail.ID.Equals(id)).FirstOrDefault();
    }
    
    // POST api/values
     public void Post([FromBody]HeaderDetail value)
    {
      // you can get all posted HeaderDetail data  in value variable.
    }

    You can send data from client to service as JSON format like below. 

    Content-Type: application/json

    {
      "Header": {
      "BatchName": "dfsdfsadf",
      "Period": "sdfsdfsdfsadf"
    },
      "Detail": {
      "ID": 102,
      "Category": "dsfsdfll",
      "Amount": 12120.340
    }
    }



    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 9, 2016 8:00 AM