locked
How to receive JSON object in POST using web api RRS feed

  • Question

  • User-1590246810 posted

    Dear All,

      I need to receive below JSON objects from mob app/postman client to process the request on WEB API. Could anyone please help me, how to retrieve this in my application.

     I had created below classes for getting root values.

    public class MembersFundsList
           {
               public string MembersID { getset; }
               public string MembersName { getset; }
               public string SourceId { getset; }
               public string SourceName { getset; }
               public string Amount { getset; }
           }
     
           public class TripObject
           {
               public string Token { getset; }
               public string TripID { getset; }
               public List<MembersFundsList> MembersFundsList { getset; }
           }

    JSON Object

    
    

    {
    "Token":"********",
    "TripID":"********",
    "MembersFundsList":[
    {
    "MembersID":"83",
    "MembersName":"xxxx",
    "SourceId":"85",
    "SourceName":"xxxxxx",
    "Amount":"0.00"
    },
    {
    "MembersID":"83",
    "MembersName":"yyyy",
    "SourceId":"85",
    "SourceName":"yyyyyyy",
    "Amount":"0.00"
    },

    {
    "MembersID":"83",
    "MembersName":"zzzz",
    "SourceId":"85",
    "SourceName":"zzzzzz",
    "Amount":"0.00"
    }
    ]
    }

    Below is my code, to retreive the JSON result from Postman client. response came to endpoint. but, can't searlize it properly. Please help me on this.. its very URGENT.

    public JObject FundAllocation(JObject jsonResult)
    {
    dynamic json = new JObject();

    lock (this)
    {
    try
    {
    string jsonString = JsonConvert.SerializeObject(jsonResult);
    var res = JArray.Parse(jsonString);
    List<FundAlloc.TripObject> items = JsonConvert.DeserializeObject<List<FundAlloc.TripObject>>(jsonString);
    }
    catch (Exception ex)
    {
    }
    }
    return json;
    }

    Monday, August 14, 2017 5:17 AM

Answers

  • User-271186128 posted

    Hi AL MUBARAK,

    I agree with bruce’s option. The json string that you have given is an class object, not a list.

    And in your code, the ‘json ’ parameter hasn’t been assigned a value.

    To convert JObject parameter to TripObject model, you could refer to the following code:

    Code in Api Controller:

    [HttpPost]        
            public JObject FundAllocation(JObject jsonResult)
            {           
                TripObject item = JsonConvert.DeserializeObject<TripObject>(jsonResult.ToString());
                return jsonResult;
            }
    

    Code in View:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="Scripts/jquery-1.10.2.min.js"></script>
        <script>
            function getData() {
                var MembersFundsList = [{
                    MembersID: "83",
                    MembersName: "m1",
                    SourceId: "85",
                    SourceName: "s1",
                    Amount:"0.00"
                   },
                    {
                        MembersID: "83",
                        MembersName: "m2",
                        SourceId: "85",
                        SourceName: "s2",
                        Amount: "0.00"
                    }
                ];
                var TripObject = {
                    Token: "token1",
                    TripID: "trip1",
                    MembersFundsList: MembersFundsList
                };
                $.ajax({
                    type: "POST",
                    url: 'http://localhost:64186/api/Discount/FundAllocation', //change to your url
                    data: JSON.stringify(TripObject) , // pass json object to web api
                    contentType: 'application/json',
                    success: function () {
                        alert('success!');
                    }            });
            }
        </script>
    </head>
    <body>
        <input type="button" value="GetData" onclick="getData()" />
    </body>
    </html>
    

    Finally, you could see the output like this:

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 16, 2017 9:01 AM

All replies

  • User-474980206 posted

    your sample JSON is a single TripObject not an array. try:

       FundAlloc.TripObject trip = JsonConvert.DeserializeObject<FundAlloc.TripObject>(jsonString);

    Monday, August 14, 2017 6:09 PM
  • User-271186128 posted

    Hi AL MUBARAK,

    I agree with bruce’s option. The json string that you have given is an class object, not a list.

    And in your code, the ‘json ’ parameter hasn’t been assigned a value.

    To convert JObject parameter to TripObject model, you could refer to the following code:

    Code in Api Controller:

    [HttpPost]        
            public JObject FundAllocation(JObject jsonResult)
            {           
                TripObject item = JsonConvert.DeserializeObject<TripObject>(jsonResult.ToString());
                return jsonResult;
            }
    

    Code in View:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="Scripts/jquery-1.10.2.min.js"></script>
        <script>
            function getData() {
                var MembersFundsList = [{
                    MembersID: "83",
                    MembersName: "m1",
                    SourceId: "85",
                    SourceName: "s1",
                    Amount:"0.00"
                   },
                    {
                        MembersID: "83",
                        MembersName: "m2",
                        SourceId: "85",
                        SourceName: "s2",
                        Amount: "0.00"
                    }
                ];
                var TripObject = {
                    Token: "token1",
                    TripID: "trip1",
                    MembersFundsList: MembersFundsList
                };
                $.ajax({
                    type: "POST",
                    url: 'http://localhost:64186/api/Discount/FundAllocation', //change to your url
                    data: JSON.stringify(TripObject) , // pass json object to web api
                    contentType: 'application/json',
                    success: function () {
                        alert('success!');
                    }            });
            }
        </script>
    </head>
    <body>
        <input type="button" value="GetData" onclick="getData()" />
    </body>
    </html>
    

    Finally, you could see the output like this:

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 16, 2017 9:01 AM