从 Json 问题中提取详细信息
Extraction of details from Json Issue
我想从 JSON 中获取状态和未映射状态,如下面所附代码中所标记。我试过了,但我做不到。任何帮助都是可观的。谢谢。
请查找附件中的代码here
请找出如下所示的错误代码:
string responseString = "";
Dictionary<string, object> input = new Dictionary<string, object>();
string MerchantKey = ConfigurationManager.AppSettings["MERCHANT_KEY"];
string SALT = ConfigurationManager.AppSettings["SALT"];
string MerchantId = MerchantTransactionId;
input.Add("key", MerchantKey.ToString());
input.Add("command", "verify_payment");
input.Add("var1", MerchantId.ToString());
input.Add("hash", GenerateHash(MerchantTransactionId));
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://info.payu.in/merchant/postservice.php?form=2");
string postData = "key=" + MerchantKey.ToString() + "&command=verify_payment&var1=" + MerchantId.ToString() + "&hash=" + GenerateHash(MerchantId);
//string postData = "key=Tn1kDH&command=verify_payment&var1=200700001&hash=" + GenerateHash();
var data1 = Encoding.ASCII.GetBytes(postData);
request.ContentLength = data1.Length;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var stream = request.GetRequestStream())
{
stream.Write(data1, 0, data1.Length);
}
HttpWebResponse orderResponse = (HttpWebResponse)request.GetResponse();
orderData = ParseResponse(orderResponse);
JObject RequestBodyData = JObject.Parse((string)orderData.SelectToken("transaction_details"));
也找出主要错误here
找到响应here
JToken.SelectToken method from Newtonsoft.Json returns JToken 对象。仅当此 JToken 对象表示的值是原始类型(例如字符串或数字)时,才能使用显式转换运算符将 JToken 对象转换为字符串。
在描述的场景中,JToken 对象表示整个“transaction_details”JSON 对象。结果,显式转换为字符串returns对应“Can not convert Object to String”。异常。
如果确实需要获取“transaction_details”JSON对象的字符串表示,那么正如评论中提到的,可以使用JToken.ToString方法。
JToken.ToString Method
Returns the indented JSON for this token.
但如果真正的任务是获取“status”和“unmappedstatus”字段的值,那么可以直接从现有的 JToken 对象中查询这些值。
var status = (string)orderData.SelectToken("transaction_details.status");
var unmappedstatus = (string)orderData.SelectToken("transaction_details.unmappedstatus");
或
var transactionDetails = orderData.SelectToken("transaction_details");
var status = (string)transactionDetails.SelectToken("status");
var unmappedstatus = (string)transactionDetails.SelectToken("unmappedstatus");
我想从 JSON 中获取状态和未映射状态,如下面所附代码中所标记。我试过了,但我做不到。任何帮助都是可观的。谢谢。
请查找附件中的代码here
请找出如下所示的错误代码:
string responseString = "";
Dictionary<string, object> input = new Dictionary<string, object>();
string MerchantKey = ConfigurationManager.AppSettings["MERCHANT_KEY"];
string SALT = ConfigurationManager.AppSettings["SALT"];
string MerchantId = MerchantTransactionId;
input.Add("key", MerchantKey.ToString());
input.Add("command", "verify_payment");
input.Add("var1", MerchantId.ToString());
input.Add("hash", GenerateHash(MerchantTransactionId));
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://info.payu.in/merchant/postservice.php?form=2");
string postData = "key=" + MerchantKey.ToString() + "&command=verify_payment&var1=" + MerchantId.ToString() + "&hash=" + GenerateHash(MerchantId);
//string postData = "key=Tn1kDH&command=verify_payment&var1=200700001&hash=" + GenerateHash();
var data1 = Encoding.ASCII.GetBytes(postData);
request.ContentLength = data1.Length;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var stream = request.GetRequestStream())
{
stream.Write(data1, 0, data1.Length);
}
HttpWebResponse orderResponse = (HttpWebResponse)request.GetResponse();
orderData = ParseResponse(orderResponse);
JObject RequestBodyData = JObject.Parse((string)orderData.SelectToken("transaction_details"));
也找出主要错误here
找到响应here
JToken.SelectToken method from Newtonsoft.Json returns JToken 对象。仅当此 JToken 对象表示的值是原始类型(例如字符串或数字)时,才能使用显式转换运算符将 JToken 对象转换为字符串。
在描述的场景中,JToken 对象表示整个“transaction_details”JSON 对象。结果,显式转换为字符串returns对应“Can not convert Object to String”。异常。
如果确实需要获取“transaction_details”JSON对象的字符串表示,那么正如评论中提到的,可以使用JToken.ToString方法。
JToken.ToString Method
Returns the indented JSON for this token.
但如果真正的任务是获取“status”和“unmappedstatus”字段的值,那么可以直接从现有的 JToken 对象中查询这些值。
var status = (string)orderData.SelectToken("transaction_details.status");
var unmappedstatus = (string)orderData.SelectToken("transaction_details.unmappedstatus");
或
var transactionDetails = orderData.SelectToken("transaction_details");
var status = (string)transactionDetails.SelectToken("status");
var unmappedstatus = (string)transactionDetails.SelectToken("unmappedstatus");