4

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~

5
  • Do you mean how to get JSON POST data in your C# code function? Commented Sep 18, 2017 at 3:40
  • have you tried JsonConvert.DeserializeObject(yourJson) you will need to include the using Newtonsoft.Json; statement in your code. Commented Sep 18, 2017 at 3:47
  • Yes @souvik, I dont know how to get JSON POST data in code behind of asp.net Commented Sep 18, 2017 at 3:49
  • 1
    Please check these links- forums.asp.net/t/… and c-sharpcorner.com/UploadFile/dacca2/…. It has step-by-step details. Commented Sep 18, 2017 at 3:53
  • So, what have you done so far? Commented Sep 18, 2017 at 5:13

4 Answers 4

2
   **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;
 }
Sign up to request clarification or add additional context in comments.

1 Comment

The C# code makes no sense, where are you extracting the json data from the request?
1

You have to parse your json in a dynamic object if you do not know about what json is coming.

You can use the dynamic object like this.

dynamic data = Json.Decode(json);

in you case you can get the data like this.

string transaction_time=data.transaction_time;

Comments

0

You can also use object instead of dynamic, and then use reflection to get all the properties in json object, I can give you generic code example, you can search on Reflection for more understanding

public void GetData(object obj)
{
    foreach(var item in obj.GetType().GetProperties())
    {
         Console.WriteLine(item.Name + ":\t" + item.GetValue(obj));
    }
} 

You can use it whatever the way you want

Comments

-1

Post using C#

var client = new RestClient("http://localhost:8080/api/subject");
var request = new RestRequest(Method.POST);

request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");

request.AddParameter("application/json", "{\n        \"author\": "Balu\",\n        \"title\": \"Post using C#\"}", ParameterType.RequestBody);

IRestResponse response = client.Execute(request);

1 Comment

No, I mean, how to get JSON POST data, not POST JSON data to C#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.