Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to Deserialize String with JSON Contents to C# Object

I have an HttpClient that makes a call to a REST API.

var response = await client.PostAsync("Payments/CreditCard", content);
var contents = await response.Content.ReadAsStringAsync();

When I read the content of the response as a string, I get the following result:

"\"{\\\"ssl_card_number\\\":\\\"41**********9994\\\",\\\"ssl_exp_date\\\":\\\"1219\\\",\\\"ssl_amount\\\":\\\"50.00\\\",\\\"ssl_salestax\\\":\\\"\\\",\\\"ssl_invoice_number\\\":\\\"\\\",\\\"ssl_departure_date\\\":\\\"\\\",\\\"ssl_completion_date\\\":\\\"\\\",\\\"Test\\\":\\\"\\\",\\\"TestField\\\":\\\"TestValue\\\",\\\"ssl_result\\\":\\\"0\\\",\\\"ssl_result_message\\\":\\\"APPROVAL\\\",\\\"ssl_approval_code\\\":\\\"578380\\\",\\\"ssl_cvv2_response\\\":\\\"U\\\",\\\"ssl_avs_response\\\":\\\"G\\\",\\\"ssl_account_balance\\\":\\\"0.00\\\",\\\"ssl_txn_time\\\":\\\"04/09/2018 09:41:01 AM\\\",\\\"ssl_card_type\\\":\\\"CREDITCARD\\\"}\""

When I debug and inspect the value of the contents variable, it contains the following:

enter image description here

When I try to deserialize the string into a C# object using JSON.Net, I receive an exception, because the contents variable can't be converted to my C# object. However, if I take the string from the Text Visualizer, I'm able to successfully convert it to my C# object.

Here's the class I'm trying to deserialize the string contents into:

public class PaymentResponse
{
     public string ssl_account_balance { get; set; }
     public string ssl_amount { get; set; }
     public string ssl_approval_code { get; set; }
     public string ssl_avs_response { get; set; }
     public string ssl_card_number { get; set; }
     public string ssl_card_type { get; set; }
     public string ssl_completion_date { get; set; }
     public string ssl_cvv2_response { get; set; }
     public string ssl_departure_date { get; set; }
     public string ssl_exp_date { get; set; }
     public string ssl_invoice_number { get; set; }
     public string ssl_result { get; set; }
     public string ssl_result_message { get; set; }
     public string ssl_salestax { get; set; }
     public string ssl_txn_id { get; set; }
     public string ssl_txn_time { get; set; }
}

Here's the code I use for deserializing:

paymentResponse = JsonConvert.DeserializeObject<PaymentResponse>(contents);

How can I get my contents variable to have the same value that appears in the Text Visualizer?

like image 458
Jade Cowan Avatar asked Dec 14 '25 02:12

Jade Cowan


2 Answers

The data shown appears to be serialized twice.

In that case it would need to be deserialized twice.

First to string,

var json = JsonConvert.DeserializeObject<string>(contents);

and then to the desired type

var paymentResponse = JsonConvert.DeserializeObject<PaymentResponse>(json);
like image 73
Nkosi Avatar answered Dec 16 '25 15:12

Nkosi


@Nkosi was right: first deserialize it to string and then to PaymentResponse:

var contents = "\"{\\\"ssl_card_number\\\":\\\"41**********9994\\\",\\\"ssl_exp_date\\\":\\\"1219\\\",\\\"ssl_amount\\\":\\\"50.00\\\",\\\"ssl_salestax\\\":\\\"\\\",\\\"ssl_invoice_number\\\":\\\"\\\",\\\"ssl_departure_date\\\":\\\"\\\",\\\"ssl_completion_date\\\":\\\"\\\",\\\"Test\\\":\\\"\\\",\\\"TestField\\\":\\\"TestValue\\\",\\\"ssl_result\\\":\\\"0\\\",\\\"ssl_result_message\\\":\\\"APPROVAL\\\",\\\"ssl_approval_code\\\":\\\"578380\\\",\\\"ssl_cvv2_response\\\":\\\"U\\\",\\\"ssl_avs_response\\\":\\\"G\\\",\\\"ssl_account_balance\\\":\\\"0.00\\\",\\\"ssl_txn_time\\\":\\\"04/09/2018 09:41:01 AM\\\",\\\"ssl_card_type\\\":\\\"CREDITCARD\\\"}\"";
var contentAsString = JsonConvert.DeserializeObject<string>(contents);
var paymentResponse = JsonConvert.DeserializeObject<PaymentResponse>(contentAsString);
Console.WriteLine(paymentResponse.ssl_card_number);

Check the fiddle.

like image 23
Anderson Pimentel Avatar answered Dec 16 '25 15:12

Anderson Pimentel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!