我是C#的新人 如何获取JSON POST数据? 我有一个POST JSON数据到我的asp.net的服务。
这里有数据:
{
"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"
}
据我所知,我可以使用WebMethod捕获所有数据,创建一个方法然后初始化每个JSON对象的参数,但是每个帖子,它们都会发送不同的JSON。所以我必须获取所有JSON并动态地将其解析为变量。
请帮助:)
感谢〜
答案 0 :(得分:0)
使用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 :(得分:0)
如果你不知道json将要发生什么,你必须在动态对象中解析你的json。
您可以像这样使用动态对象。
dynamic data = Json.Decode(json);
在你的情况下,你可以得到这样的数据。
string transaction_time=data.transaction_time;
答案 2 :(得分:0)
你也可以使用object而不是dynamic,然后使用反射来获取json对象中的所有属性,我可以给你一般的代码示例,你可以在Reflection上搜索以获得更多的理解
public void GetData(object obj)
{
foreach(var item in obj.GetType().GetProperties())
{
Console.WriteLine(item.Name + ":\t" + item.GetValue(obj));
}
}
您可以按照您想要的方式使用它
答案 3 :(得分:0)
**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);
}
});
在Asp.net代码后面或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;
}