braindice
braindice

Reputation: 988

WebAPI convert nested JSON to string

I have a somewhat complex JSON coming into a WEBAPI -

{
    "TypeInq": "ResidentialStart",
    "DataInq":  {
        "Account_ID": "2491054657",
        "Premise_ID": "5394660000",
        "DogWarning": "N",
        "ServiceStartDate": "2020-01-10",
        "PrimaryPerson": {
            "PersonId": "0357801000",
            "POS_ID_Required": "Y",
            "LastName": "HOGAN",
            "FirstName": "BARRY L",
            "Username": "[email protected]",
            "EmailList": [
                {
                    "Email": "[email protected]",
                    "EmailType": "PERSONAL EMAIL",
                    "PrimaryEmailFlag": "Y",
                    "ActionFlag": "ADD",
                    "ActiveInactiveFlag": "C1AC"
                }
            ],
            "DateOfBirth": "1977-11-04",
            "CurrentAddress": {
                "AddressLine1": "1923 Chestnut Dr",
                "City": "Pleasant hill ",
                "State": "MO",
                "Zip": "64080"
            },
            "MailingAddress": {
                "PremiseID": "5394660000"
            }
        },
        "AdditionalPersons": [
            {
                "PersonId": "0357801000",
                "POS_ID_Required": "Y",
                "LastName": "HOGAN",
                "FirstName": "BARRY L",
                "Username": "[email protected]",
                "EmailList": [
                    {
                        "Email": "[email protected]",
                        "EmailType": "PERSONAL EMAIL",
                        "PrimaryEmailFlag": "Y",
                        "ActionFlag": "ADD",
                        "ActiveInactiveFlag": "C1AC"
                    }
                ],
                "DateOfBirth": "1977-11-04",
                "CurrentAddress": {
                    "AddressLine1": "1923 Chestnut Dr",
                    "City": "Pleasant hill ",
                    "State": "MO",
                    "Zip": "64080"
                },
                "MailingAddress": {
                    "PremiseID": "5394660000"
                }
            }
        ]
    }
}

So the part 'TypeInq' seems to be fine The Part 'DataInq' i need to convert all that into a string before I process it. Not 100% sure how to approach this in C# but was trying this where the 'Base' object just has strings in it for 'TypeInq' and 'DataInq'

 [HttpPost]
        [Route("CreateInquiry")]
        public IHttpActionResult CreateNewInquiry(Base inqBasic)
        {

            string retval = "";
            try
            {


            //return Ok();

               InquiryService svc = new InquiryService();
               retval = svc.CreateInquiry(inqBasic);
            }
            catch (Exception ex)
            {
                Logger.LogEx(ex);
                return WebApiConfig.ExceptionResponse(this);
            }
            return Ok(retval);

        }

Upvotes: 0

Views: 34

Answers (1)

workingmantypething
workingmantypething

Reputation: 121

You can directly deserialize the incoming json object. Then just reference the appropriate property you want the value of.

object somenewobject = new JavaScriptSerializer().Deserialize(response.Data.ToString());

Upvotes: 1

Related Questions