Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get JSON POST data in C#

im new in C#.
How to get JSON POST data? I have an service that POST JSON data to my asp.net.

Here the data:

{
   "transaction_time": "2017-09-18 09:47:54",
   "transaction_status": "capture",
   "transaction_id": "b7c8cfa9-b706-4a9d-b70d-8d70149145bd",
   "status_message": "Veritrans payment notification",
   "status_code": "200",
   "signature_key": "b22b5740bf2c587ba949ae5343757a66e5a75c45b9377b9ee891909bbd8977bb2caea8e0549bf09b5040b22f54efc76aa8beb31e321f9d600f267b23f37b30ae",
   "payment_type": "credit_card",
   "order_id": "LUHUMMLWOT",
   "masked_card": "401111-1112",
   "gross_amount": "326000.00",
   "fraud_status": "accept",
   "bank": "mandiri",
   "approval_code": "1505702878234"
 }

As far I know, I can catch all data using WebMethod, create an method then initialize the parameter each JSON object, but every post, they send different JSON. So I must get all JSON and parse it into variable dynamically.

Please help :)

Thanks~

like image 680
Raissa Ditya Putri Avatar asked Oct 21 '25 12:10

Raissa Ditya Putri


1 Answers

   **Pleas do it step by step as below
    in Your Asp.net Java script using**



var yourPostJsonObject={
   "transaction_time": "2017-09-18 09:47:54",
   "transaction_status": "capture",
   "transaction_id": "b7c8cfa9-b706-4a9d-b70d-8d70149145bd",
   "status_message": "Veritrans payment notification",
   "status_code": "200",
   "signature_key": "b22b5740bf2c587ba949ae5343757a66e5a75c45b9377b9ee891909bbd8977bb2caea8e0549bf09b5040b22f54efc76aa8beb31e321f9d600f267b23f37b30ae",
   "payment_type": "credit_card",
   "order_id": "LUHUMMLWOT",
   "masked_card": "401111-1112",
   "gross_amount": "326000.00",
   "fraud_status": "accept",
   "bank": "mandiri",
   "approval_code": "1505702878234"
 }


$.ajax({
                type: 'POST',
                dataType: 'json',
                cache: false,
                url: 'SendTransaction', // webmethod or web serivces URL
                data: {jsonReceiverInCsharpObjecName:JSON.stringify(yourPostJsonObject)},
                success: function (response) {
                   alert('scucess')
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert('Error - ' + errorThrown);
                }
            });

in Asp.net Code behind or webservice

using Newtonsoft.Json;
using System.Dynamic;
using Newtonsoft.Json.Converters;

public bool SendTransaction(string jsonReceiverInCsharpObjecName)
 {
   dynamic dynData =JsonConvert.DeserializeObject<ExpandoObject>
   (jsonReceiverInCsharpObjecName, new ExpandoObjectConverter());

foreach (KeyValuePair<string, object> transItem in dynData 
{
   if (transItem.Key == "transaction_time")
   var transaction_time = Convert.ToString(transItem.Value);
   else if (transItem.Key == "transaction_status")
   var transaction_status = Convert.ToString(transItem.Value);
   else if (transItem.Key == "transaction_id")
   var transaction_ido = Convert.ToString(transItem.Value);
   //else
   //do for rest attribute of your json data
 }

return true;
 }
like image 150
Hiren Patel Avatar answered Oct 23 '25 00:10

Hiren Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!