Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I did the
HttpWebRequest
and got the following HttpWebResponse:

response=1&responsetext=SUCCESS&authcode=123456&transactionid=8607693510&avsresponse=N&cvvresponse=N&orderid=&type=sale&response_code=100


i want to convert this response into the Class object which is followig:
public class ResponseModel
  {
      public string response { get; set; }
      public string responsetext { get; set; }
      public string authcode { get; set; }
      public string transactionid { get; set; }
      public string avsresponse { get; set; }
      public string cvvresponse { get; set; }
      public string orderid { get; set; }
      public string type { get; set; }
      public string response_code { get; set; }
  }


What I have tried:

var data=
response=1&responsetext=SUCCESS&authcode=123456&transactionid=8607693510&avsresponse=N&cvvresponse=N&orderid=&type=sale&response_code=100
;
var resModel = JsonConvert.DeserializeObject<ResponseModel>(data);
Posted
Updated 8-Aug-23 6:17am
Comments
Member 15627495 8-Aug-23 12:01pm    
It's not a JSON expression.

It's URI parameters. var_name=var_value

split this string with '&' in a first step, then split all contents by '='

1 solution

JSON data looks like this:
JSON
{"name":"John", "age":30, "car":null}
Your data is an HTML Query string, where each term is separated by ampersands, each of which contains key and value parts seperated by equal signs.

To process this, use string.Split to break it on the ampersands:
C#
string[] terms = input.Split("&");
Then loop through each separating them into key and value:
C#
foreach (string term in terms)
   {
   string[] parts = term.Split("=");
   ...
   }
You could then use a switch to decide what each key is and thus what you need to do with the values:
C#
switch(parts[0].ToLower())
   {
   default: throw new (ArgumentException($"Unknown key: {parts[0]"));
   case "response": 
      ...
      break;
   ...
   }
 
Share this answer
 
v2
Comments
Member 10371658 9-Aug-23 6:31am    
Thanks, it's worked for me.
OriginalGriff 9-Aug-23 6:49am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900