I want to Fetch the of status and unmappedstatus from JSON as marked in the code attached below. I tried but I am unable to do it. Any help is appreciable. Thank you.
Please find the code attached here
Please find the error code shown below:
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"));
Also find the main error here
Find the response here
CodePudding user response:
The JToken.SelectToken method from Newtonsoft.Json returns JToken object. JToken object can be converted to string using explicit conversion operator only when value representing by this JToken object is a primitive type (e.g. string or number).
In the described scenario JToken object represents the entire "transaction_details" JSON object. As result, the explicit conversion to string returns corresponding "Can not convert Object to String." exception.
If there is a real need to get a string representation of "transaction_details" JSON object, then as it was mentioned in comments, the JToken.ToString method can be used.
JToken.ToString Method
Returns the indented JSON for this token.
But if the real task is to get values of "status" and "unmappedstatus" fields, then these values can be queried directly from the existing JToken object.
var status = (string)orderData.SelectToken("transaction_details.status");
var unmappedstatus = (string)orderData.SelectToken("transaction_details.unmappedstatus");
OR
var transactionDetails = orderData.SelectToken("transaction_details");
var status = (string)transactionDetails.SelectToken("status");
var unmappedstatus = (string)transactionDetails.SelectToken("unmappedstatus");