给定 json 作为响应,但不确定如何解析它



我正在使用此代码尝试从支付网关解析我的响应:

var responseMessage = client.PostAsJsonAsync("transaction", transData).Result;
var response = responseMessage.Content.ReadAsStringAsync().Result;
MessageBox.Show(response);
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
var objCustomer = jsonSerializer.Deserialize<ReturnValues.RootObject>(response);

这是我正在尝试创建的类:

public class ReturnValues
{
public class Card
{
public string id { get; set; }
public string card_type { get; set; }
public string first_six { get; set; }
public string last_four { get; set; }
public string masked_card { get; set; }
public string expiration_date { get; set; }
public string status { get; set; }
public string auth_code { get; set; }
public string processor_response_code { get; set; }
public string processor_response_text { get; set; }
public string processor_type { get; set; }
public string processor_id { get; set; }
public string avs_response_code { get; set; }
public string cvv_response_code { get; set; }
public DateTime created_at { get; set; }
public DateTime updated_at { get; set; }
}
public class Response
{
public Card card { get; set; }
}
public class BillingAddress
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_line_1 { get; set; }
public string address_line_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string country { get; set; }
public string phone { get; set; }
public string fax { get; set; }
public string email { get; set; }
}
public class ShippingAddress
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_line_1 { get; set; }
public string address_line_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string country { get; set; }
public string phone { get; set; }
public string fax { get; set; }
public string email { get; set; }
}
public class Data
{
public string id { get; set; }
public string type { get; set; }
public int amount { get; set; }
public int tax_amount { get; set; }
public bool tax_exempt { get; set; }
public int shipping_amount { get; set; }
public int discount_amount { get; set; }
public string payment_adjustment_type { get; set; }
public int payment_adjustment_value { get; set; }
public string currency { get; set; }
public string description { get; set; }
public string order_id { get; set; }
public string po_number { get; set; }
public string ip_address { get; set; }
public bool email_receipt { get; set; }
public string email_address { get; set; }
public string payment_method { get; set; }
public Response response { get; set; }
public string status { get; set; }
public int response_code { get; set; }
public string customer_id { get; set; }
public BillingAddress billing_address { get; set; }
public ShippingAddress shipping_address { get; set; }
public DateTime created_at { get; set; }
public DateTime updated_at { get; set; }
}
public class RootObject
{
public string status { get; set; }
public string msg { get; set; }
public Data data { get; set; }
}
}

这是我得到的回应:

"{\"状态\":\"成功",\"msg\":\"成功",\"数据":{\"id\":\">

bp2pa41erttupu3q1eng\",\"user_id\":\"bmibms9erttqdc2kigl0\",\"user_name\":\"dev\",\"merchant_id\":\"asdfasdf",\"idempotency_key":\",\"idempotency_time":0,\"type\":\"sale",\"amount\":1000,\"base_amount\":1000,\"amount_authorized\":1000,\"amount_captured\":1000,\"amount_settled\":0,\"amount_refunded\":0,\"payment_adjustment\":0,\"tip_amount":0,\"settlement_batch_id\":\"\",\"processor_id\":\"BMIbnfperttqdc2kigmg\",\"processor_type\":\"tsys_sierra\",\"processor_name\":\"Keyed 信用 卡\",\"payment_method\":\"卡",\"payment_type":\"卡",\"功能":[\"avs\",\"card_verification\",\"levelii",\"fake_response\"],\"national_tax_amount\":0,\"duty_amount\":0,\"ship_from_postal_code":\"\",\"summary_commodity_code":\"\",\"merchant_vat_registration_number:\"\",\"customer_vat_registration_number\":\"\",\"tax_amount\":0,"tax_exempt\":假,"shipping_amount":0,"附加费":0,\"discount_amount":0,"货币":"美元",\"说明\":\"这 是一个 test\",\"order_id\":\"555555\",\"po_number\": \"666666",\"ip_address\":\"1.1.1.1",\"transaction_source":\"api",\"email_receipt\":false,\"email_address":\"\",\"customer_id\":\"\",customer_payment_type\":\"\",customer_payment_ID\":\"\",subscription_id\":\"\",\"referenced_transaction_id":\"\",\"response_body\":{\"card\":{\"id\":\"BP2PA41erttupu3Q1eo0\",\"card_type\":\"签证",\"first_six":\"401288",\"last_four":\"1881\","masked_card\":\"401288******1881\",expiration_date\":\"12/21",\"响应":\"已批准",\"response_code\":100,\"auth_code":\"TAS000",\"processor_response_code":\"00",\"processor_response_text\":\"批准 TAS000 \",processor_transaction_id\":\"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\"processor_type\":\"tsys_sierra",\"processor_id\":\"bmgwgtgtherhemg\",\"avs_response_code\":\"M\",\"cvv_response_code\":\"M\",\"processor_specific\":null,\"created_at\":\"2020-02-13T18:27:28.149933095Z\",\"updated_at\":\"2020-02-13T18:27:28.196367669Z\"}},\"custom_fields\":{},\"line_items\":null,\"状态":\"pending_settlement",\"响应":\"已批准\",\"response_code\":100,\"billing_address\":{\"first_name":约翰",last_name":"Doe","公司":\"address_line_1":123 一些 街道\",\"address_line_2\":\"\",\"城市\":\"贝瑟默",\"州\": \"AL",\"postal_code\":\"35020",\"国家"::"美国","电话":5555555555",传真:\",电子邮件":"test@gmail.com"},\"shipping_address":{\"first_name":\",\"last_name":\",\"公司":\",\"address_line_1":\"\",\"address_line_2":\",\"城市":\","州":\","postal_code":\","国家":\","电话":\",\"传真":\",\"电子邮件":\"},\"created_at\"::"2020-02-13T18:27:28.104845544Z\",\"updated_at\":\"2020-02-13T18:27:28.200963331Z\",\"captured_at\":\"2020-02-13T18:27:28.200962679Z\",\"settled_at\":null}}">

我收到此消息:

System.InvalidOperationException: '无法转换类型的对象 "System.String"键入"Test.ReturnValues+Response">

我不确定我做错了什么。我使用在线 json 转换器来上课。这似乎是正确的。

有什么建议吗?

我使用 json2csharp.com 来生成C#类,它提出了一些不同的东西。

public class ReturnValues
{
public class Card
{
public string id { get; set; }
public string card_type { get; set; }
public string first_six { get; set; }
public string last_four { get; set; }
public string masked_card { get; set; }
public string expiration_date { get; set; }
public string response { get; set; }
public int response_code { get; set; }
public string auth_code { get; set; }
public string processor_response_code { get; set; }
public string processor_response_text { get; set; }
public string processor_transaction_id { get; set; }
public string processor_type { get; set; }
public string processor_id { get; set; }
public string avs_response_code { get; set; }
public string cvv_response_code { get; set; }
public object processor_specific { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
}
public class ResponseBody
{
public Card card { get; set; }
}
public class CustomFields
{
}
public class BillingAddress
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_line_1 { get; set; }
public string address_line_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string country { get; set; }
public string phone { get; set; }
public string fax { get; set; }
public string email { get; set; }
}
public class ShippingAddress
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_line_1 { get; set; }
public string address_line_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string country { get; set; }
public string phone { get; set; }
public string fax { get; set; }
public string email { get; set; }
}
public class Data
{
public string id { get; set; }
public string user_id { get; set; }
public string user_name { get; set; }
public string merchant_id { get; set; }
public string idempotency_key { get; set; }
public int idempotency_time { get; set; }
public string type { get; set; }
public int amount { get; set; }
public int base_amount { get; set; }
public int amount_authorized { get; set; }
public int amount_captured { get; set; }
public int amount_settled { get; set; }
public int amount_refunded { get; set; }
public int payment_adjustment { get; set; }
public int tip_amount { get; set; }
public string settlement_batch_id { get; set; }
public string processor_id { get; set; }
public string processor_type { get; set; }
public string processor_name { get; set; }
public string payment_method { get; set; }
public string payment_type { get; set; }
public List<string> features { get; set; }
public int national_tax_amount { get; set; }
public int duty_amount { get; set; }
public string ship_from_postal_code { get; set; }
public string summary_commodity_code { get; set; }
public string merchant_vat_registration_number { get; set; }
public string customer_vat_registration_number { get; set; }
public int tax_amount { get; set; }
public bool tax_exempt { get; set; }
public int shipping_amount { get; set; }
public int surcharge { get; set; }
public int discount_amount { get; set; }
public string currency { get; set; }
public string description { get; set; }
public string order_id { get; set; }
public string po_number { get; set; }
public string ip_address { get; set; }
public string transaction_source { get; set; }
public bool email_receipt { get; set; }
public string email_address { get; set; }
public string customer_id { get; set; }
public string customer_payment_type { get; set; }
public string customer_payment_ID { get; set; }
public string subscription_id { get; set; }
public string referenced_transaction_id { get; set; }
public ResponseBody response_body { get; set; }
public CustomFields custom_fields { get; set; }
public object line_items { get; set; }
public string status { get; set; }
public string response { get; set; }
public int response_code { get; set; }
public BillingAddress billing_address { get; set; }
public ShippingAddress shipping_address { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string captured_at { get; set; }
public object settled_at { get; set; }
}
public class RootObject
{
public string status { get; set; }
public string msg { get; set; }
public Data data { get; set; }
}
}

不过它似乎有效。我做了一个快速比较,有很多差异。可能值得一试。

使用jsoup它是一个免费的框架,它可以轻松地将文档解析为json格式

`list<Element> news;
Document doc = Jsoup.connect("https://en.wikipedia.org/").get();
log(doc.title());
Elements newsHeadlines = doc.select("#mp-itn b a");
for (Element headline : news) {
log("%snt%s", 
headline.attr("title"), headline.absUrl("href"));
}`

相关内容

  • 没有找到相关文章

最新更新