locked
Web api listener accept json similar to below example RRS feed

  • Question

  • User-1341450215 posted

    I am new to c# and web api . Suppose my web api listener want to accept below structured data how should I write the c# method

    {
    "Patient": {
    "UniqueID": "WH555444",
    "Name": {
    "LastName": "Doe",
    "FirstName": "John",
    "Middle": "Frank"
    },
    "OrderId":"86023",
    "Labs":[{

    "Setid": "2",
    "valuetype": "ED",
    "ObservationIdentifier": "6690-2^WBC^LN",

    "ObservationValue": "8.08",

    "ObservResultStatus": "F",

    "ReferencesRange": "4.00-10.00",
    "AbnormalFlags": "A",

    "Units": "10*9/L",

    "UserDefinedAccessChecks": "E"
    },
    {

    "Setid": "2",
    "valuetype": "ED",
    "ObservationIdentifier": "6690-2^WBC^LN",

    "ObservationValue": "8.08",

    "ObservResultStatus": "F",

    "ReferencesRange": "4.00-10.00",
    "AbnormalFlags": "A",

    "Units": "10*9/L",

    "UserDefinedAccessChecks": "E"
    }]
    }
    }

    Monday, August 29, 2016 5:09 PM

Answers

  • User-1404113929 posted

    hi,

    not a problem you can try with bellow code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using Newtonsoft.Json.Serialization;
    using Newtonsoft.Json;
    using System.Web.Script.Serialization;
    using Newtonsoft.Json.Linq;
    
    namespace api.Controllers
    {
        public class DefaultController : ApiController
        {
            [HttpPost]
            public RootObject getDetails([FromBody]string bodyText)
            {
                
                var myDeserializedObj = JObject.Parse(bodyText).ToObject<RootObject>();
                return myDeserializedObj;
            }
        }
        public class Lab
        {
            public string Setid { get; set; }
            public string valuetype { get; set; }
            public string ObservationIdentifier { get; set; }
            public string ObservationValue { get; set; }
            public string ReferencesRange { get; set; }
            public string AbnormalFlags { get; set; }
            public string ObservResultStatus { get; set; }
        }
    
        public class RootObject
        {
            public string UniqueID { get; set; }
            public string LastName { get; set; }
            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            public string OrderId { get; set; }
            public List<Lab> Labs { get; set; }
        }
    }
    

    put a break point inside getdetails action .

    thanks,

    murali

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 30, 2016 12:49 PM

All replies

  • User-1341450215 posted

    This is what I have created class from online tool.  After accessing data  I will insert to data base table. 

    public class Name
    {
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Middle { get; set; }
    }

    public class Lab
    {
    public string Setid { get; set; }
    public string valuetype { get; set; }
    public string ObservationIdentifier { get; set; }
    public string ObservationValue { get; set; }
    public string ObservResultStatus { get; set; }
    public string ReferencesRange { get; set; }
    public string AbnormalFlags { get; set; }
    public string Units { get; set; }
    public string UserDefinedAccessChecks { get; set; }
    }

    public class Patient
    {
    public string UniqueID { get; set; }
    public Name Name { get; set; }
    public string OrderId { get; set; }
    public IList<Lab> Labs { get; set; }
    }

    public class Example
    {
    public Patient Patient { get; set; }
    }

    Monday, August 29, 2016 5:11 PM
  • User36583972 posted

    Hi shuhaibk54,

    Suppose my web api listener want to accept below structured data how should I write the c# method

    You can use JsonConvert.SerializeObject() method to serialize your Object to a JSON data, then send it to your web api listener. You can refer the following method.

         using (var httpClient = new HttpClient())
                {
                    Example examples = new Example();
                    Patient parent = new Patient() { Name = new Name() { FirstName = "1", LastName = "2", Middle = "0" }, OrderId = "001", UniqueID = "12588", Labs = new List<Lab>() { new Lab() { Setid = "lab01", Units = "hellow" }, new Lab() { Setid = "lab02", Units = "hellow2" } } };
                    examples.Patient = parent;
                    var baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
                    var uri = new Uri(baseUrl + "/api/Values");
                    string contents = JsonConvert.SerializeObject(parent);
                    var response = httpClient.PostAsync(uri, new StringContent(contents, Encoding.UTF8, "application/json"));
                    response.Wait();
                    if (response.Result.IsSuccessStatusCode)
                    {
                       
                    }
    
                }

    Best Regards,

    Yohann Lu

    Tuesday, August 30, 2016 5:21 AM
  • User-1404113929 posted

    hi,

    you can make it in 2 ways,

    1) accept it as string and convert to json dictionary in controller .

    2)make classes as per object .

    thanks,

    murali

    Tuesday, August 30, 2016 9:27 AM
  • User-1341450215 posted

    Thanks for your response murali

     any example you have ?

    Thanks,

    Shuhaib

    Tuesday, August 30, 2016 10:34 AM
  • User-1404113929 posted

    hi, 

    please find bellow code.

    put this method inside that class.

     public static Dictionary<string, object> ListToDictionary<T>(this T values, string keyPropertyName= "Name",string valuePropertyName = "Value") where T : IEnumerable
            {
                Dictionary<string, object> result = new Dictionary<string, object>();
                foreach (var val in values as IEnumerable)
                {
                    var keyProperty = val.GetType().GetProperty(keyPropertyName);
                    var valueProperty = val.GetType().GetProperty(valuePropertyName);
                    //((Newtonsoft.Json.Linq.JProperty)val).Name
    
                    result.Add(keyProperty.GetValue(val,null).ToString(), valueProperty.GetValue(val,null));
                }
                return result;
            }

    and 

    var jObject= JObject.Parse(jsonData).ToList().ListToDictionary();

    thanks,

    murali.

    Tuesday, August 30, 2016 10:41 AM
  • User-1404113929 posted

    hi ,

    use bellow url for generating classes as per your json data.

    http://json2csharp.com/

    thanks,

    murali.

    Tuesday, August 30, 2016 10:42 AM
  • User-1341450215 posted

    Basically when I receive data as format below I want to insert to database table . Nothing to do with that . Suppose we are passing  below value from fidler/restapi tool , how my method would be look like  for accepting . after accepting i will insert to table . Hope you got my question  . I think it easy . As I am beginner  , I  wonder  . Sorry if you really hesitate on this .

    {
    "Patient": {
    "UniqueID": "WH555444",
    "Name": {
    "LastName": "Doe",
    "FirstName": "John",
    "Middle": "Frank"
    },
    "OrderId":"86023",
    "Labs":[{

    "Setid": "2",
    "valuetype": "ED",
    "ObservationIdentifier": "6690-2^WBC^LN",

    "ObservationValue": "8.08",

    "ObservResultStatus": "F",

    "ReferencesRange": "4.00-10.00",
    "AbnormalFlags": "A",

    "Units": "10*9/L",

    "UserDefinedAccessChecks": "E"
    },
    {

    "Setid": "2",
    "valuetype": "ED",
    "ObservationIdentifier": "6690-2^WBC^LN",

    "ObservationValue": "8.08",

    "ObservResultStatus": "F",

    "ReferencesRange": "4.00-10.00",
    "AbnormalFlags": "A",

    "Units": "10*9/L",

    "UserDefinedAccessChecks": "E"
    }]
    }
    }

     

    -------------------------------Below is the real answer -------------

    {
      "UniqueID" : "114804",
      "LastName" : "HAFEEZ",
      "FirstName" : "FAIZAN",
      "MiddleName" : "",
      "OrderId" : "86201",
      "Labs" : [
        {
          "Setid" : "1",
          "valuetype" : "IS",
          "ObservationIdentifier" : "08001^Take Mode",
          "ObservationValue" : "A",
          "ReferencesRange" : "",
          "AbnormalFlags" : "",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "2",
          "valuetype" : "IS",
          "ObservationIdentifier" : "08002^Blood Mode",
          "ObservationValue" : "W",
          "ReferencesRange" : "",
          "AbnormalFlags" : "",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "3",
          "valuetype" : "IS",
          "ObservationIdentifier" : "08003^Test Mode",
          "ObservationValue" : "CBC+DIFF",
          "ReferencesRange" : "",
          "AbnormalFlags" : "",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "4",
          "valuetype" : "IS",
          "ObservationIdentifier" : "01002^Ref Group",
          "ObservationValue" : "Man",
          "ReferencesRange" : "",
          "AbnormalFlags" : "",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "5",
          "valuetype" : "NM",
          "ObservationIdentifier" : "30525-0^Age",
          "ObservationValue" : "27",
          "ReferencesRange" : "",
          "AbnormalFlags" : "",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "6",
          "valuetype" : "NM",
          "ObservationIdentifier" : "6690-2^WBC",
          "ObservationValue" : "7.52",
          "ReferencesRange" : "4.00-11.00",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "7",
          "valuetype" : "NM",
          "ObservationIdentifier" : "704-7^BAS#",
          "ObservationValue" : "0.03",
          "ReferencesRange" : "0.00-0.10",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "8",
          "valuetype" : "NM",
          "ObservationIdentifier" : "706-2^BAS%",
          "ObservationValue" : "0.4",
          "ReferencesRange" : "0.0-1.0",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "9",
          "valuetype" : "NM",
          "ObservationIdentifier" : "751-8^NEU#",
          "ObservationValue" : "3.38",
          "ReferencesRange" : "2.00-7.00",
          "AbnormalFlags" : "<OBX.8.1>N</OBX.8.1><OBX.8.1>A</OBX.8.1>",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "10",
          "valuetype" : "NM",
          "ObservationIdentifier" : "770-8^NEU%",
          "ObservationValue" : "44.9",
          "ReferencesRange" : "50.0-70.0",
          "AbnormalFlags" : "<OBX.8.1>L</OBX.8.1><OBX.8.1>A</OBX.8.1>",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "11",
          "valuetype" : "NM",
          "ObservationIdentifier" : "711-2^EOS#",
          "ObservationValue" : "0.54",
          "ReferencesRange" : "0.02-0.50",
          "AbnormalFlags" : "<OBX.8.1>H</OBX.8.1><OBX.8.1>A</OBX.8.1>",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "12",
          "valuetype" : "NM",
          "ObservationIdentifier" : "713-8^EOS%",
          "ObservationValue" : "7.3",
          "ReferencesRange" : "0.5-5.0",
          "AbnormalFlags" : "<OBX.8.1>H</OBX.8.1><OBX.8.1>A</OBX.8.1>",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "13",
          "valuetype" : "NM",
          "ObservationIdentifier" : "731-0^LYM#",
          "ObservationValue" : "3.05",
          "ReferencesRange" : "0.80-4.00",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "14",
          "valuetype" : "NM",
          "ObservationIdentifier" : "736-9^LYM%",
          "ObservationValue" : "40.5",
          "ReferencesRange" : "20.0-40.0",
          "AbnormalFlags" : "H",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "15",
          "valuetype" : "NM",
          "ObservationIdentifier" : "742-7^MON#",
          "ObservationValue" : "0.52",
          "ReferencesRange" : "0.12-1.20",
          "AbnormalFlags" : "<OBX.8.1>N</OBX.8.1><OBX.8.1>A</OBX.8.1>",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "16",
          "valuetype" : "NM",
          "ObservationIdentifier" : "5905-5^MON%",
          "ObservationValue" : "6.9",
          "ReferencesRange" : "3.0-12.0",
          "AbnormalFlags" : "<OBX.8.1>N</OBX.8.1><OBX.8.1>A</OBX.8.1>",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "17",
          "valuetype" : "NM",
          "ObservationIdentifier" : "26477-0^*ALY#",
          "ObservationValue" : "0.01",
          "ReferencesRange" : "0.00-0.20",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "18",
          "valuetype" : "NM",
          "ObservationIdentifier" : "13046-8^*ALY%",
          "ObservationValue" : "0.1",
          "ReferencesRange" : "0.0-2.0",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "19",
          "valuetype" : "NM",
          "ObservationIdentifier" : "10000^*LIC#",
          "ObservationValue" : "0.07",
          "ReferencesRange" : "0.00-0.20",
          "AbnormalFlags" : "<OBX.8.1>N</OBX.8.1><OBX.8.1>A</OBX.8.1>",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "20",
          "valuetype" : "NM",
          "ObservationIdentifier" : "10001^*LIC%",
          "ObservationValue" : "1.0",
          "ReferencesRange" : "0.0-2.5",
          "AbnormalFlags" : "<OBX.8.1>N</OBX.8.1><OBX.8.1>A</OBX.8.1>",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "21",
          "valuetype" : "NM",
          "ObservationIdentifier" : "789-8^RBC",
          "ObservationValue" : "5.10",
          "ReferencesRange" : "4.00-5.50",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "22",
          "valuetype" : "NM",
          "ObservationIdentifier" : "718-7^HGB",
          "ObservationValue" : "13.9",
          "ReferencesRange" : "12.0-16.0",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "23",
          "valuetype" : "NM",
          "ObservationIdentifier" : "787-2^MCV",
          "ObservationValue" : "84.1",
          "ReferencesRange" : "80.0-100.0",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "24",
          "valuetype" : "NM",
          "ObservationIdentifier" : "785-6^MCH",
          "ObservationValue" : "27.2",
          "ReferencesRange" : "27.0-34.0",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "25",
          "valuetype" : "NM",
          "ObservationIdentifier" : "786-4^MCHC",
          "ObservationValue" : "32.3",
          "ReferencesRange" : "32.0-36.0",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "26",
          "valuetype" : "NM",
          "ObservationIdentifier" : "788-0^RDW-CV",
          "ObservationValue" : "12.0",
          "ReferencesRange" : "11.0-16.0",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "27",
          "valuetype" : "NM",
          "ObservationIdentifier" : "21000-5^RDW-SD",
          "ObservationValue" : "42.4",
          "ReferencesRange" : "35.0-56.0",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "28",
          "valuetype" : "NM",
          "ObservationIdentifier" : "4544-3^HCT",
          "ObservationValue" : "42.9",
          "ReferencesRange" : "40.0-54.0",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "29",
          "valuetype" : "NM",
          "ObservationIdentifier" : "777-3^PLT",
          "ObservationValue" : "264",
          "ReferencesRange" : "150-450",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "30",
          "valuetype" : "NM",
          "ObservationIdentifier" : "32623-1^MPV",
          "ObservationValue" : "8.5",
          "ReferencesRange" : "6.5-12.0",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "31",
          "valuetype" : "NM",
          "ObservationIdentifier" : "32207-3^PDW",
          "ObservationValue" : "16.0",
          "ReferencesRange" : "9.0-17.0",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "32",
          "valuetype" : "NM",
          "ObservationIdentifier" : "10002^PCT",
          "ObservationValue" : "0.225",
          "ReferencesRange" : "0.108-0.282",
          "AbnormalFlags" : "N",
          "ObservResultStatus" : "F"
        },
        {
          "Setid" : "33",
          "valuetype" : "IS",
          "ObservationIdentifier" : "17790-7^WBC Left Shift?",
          "ObservationValue" : "T",
          "ReferencesRange" : "",
          "AbnormalFlags" : "",
          "ObservResultStatus" : "F"
        }
      ]
    }
    Tuesday, August 30, 2016 11:01 AM
  • User-1404113929 posted

    hi,

    not a problem you can try with bellow code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using Newtonsoft.Json.Serialization;
    using Newtonsoft.Json;
    using System.Web.Script.Serialization;
    using Newtonsoft.Json.Linq;
    
    namespace api.Controllers
    {
        public class DefaultController : ApiController
        {
            [HttpPost]
            public RootObject getDetails([FromBody]string bodyText)
            {
                
                var myDeserializedObj = JObject.Parse(bodyText).ToObject<RootObject>();
                return myDeserializedObj;
            }
        }
        public class Lab
        {
            public string Setid { get; set; }
            public string valuetype { get; set; }
            public string ObservationIdentifier { get; set; }
            public string ObservationValue { get; set; }
            public string ReferencesRange { get; set; }
            public string AbnormalFlags { get; set; }
            public string ObservResultStatus { get; set; }
        }
    
        public class RootObject
        {
            public string UniqueID { get; set; }
            public string LastName { get; set; }
            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            public string OrderId { get; set; }
            public List<Lab> Labs { get; set; }
        }
    }
    

    put a break point inside getdetails action .

    thanks,

    murali

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 30, 2016 12:49 PM