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

Given json as a response but not sure how to parse it

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

 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);

这是我要创建的 class:

  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; }
    }
}

这是我得到的回复:

"{\"status\":\"success\",\"msg\":\"success\",\"data\":{\"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 Credit Cards\",\"payment_method\":\"card\",\"payment_type\":\"card\",\"features\":[\"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\":false,\"shipping_amount\":0,\"surcharge\":0,\"discount_amount\":0,\"currency\":\"usd\",\"description\":\"This is a 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\":\"visa\",\"first_six\":\"401288\",\"last_four\":\"1881\",\"masked_card\":\"401288******1881\",\"expiration_date\":\"12/21\",\"response\":\"approved\",\"response_code\":100,\"auth_code\":\"TAS000\",\"processor_response_code\":\"00\",\"processor_response_text\":\"APPROVAL TAS000 \",\"processor_transaction_id\":\"000000000000000\",\"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,\"status\":\"pending_settlement\",\"response\":\"approved\",\"response_code\":100,\"billing_address\":{\"first_name\":\"John\",\"last_name\":\"Doe\",\"company\":\"\",\"address_line_1\":\"123 Some Street\",\"address_line_2\":\"\",\"city\":\"Bessemer\",\"state\":\"AL\",\"postal_code\":\"35020\",\"country\":\"US\",\"phone\":\"5555555555\",\"fax\":\"\",\"email\":\"test@gmail.com\"},\"shipping_address\":{\"first_name\":\"\",\"last_name\":\"\",\"company\":\"\",\"address_line_1\":\"\",\"address_line_2\":\"\",\"city\":\"\",\"state\":\"\",\"postal_code\":\"\",\"country\":\"\",\"phone\":\"\",\"fax\":\"\",\"email\":\"\"},\"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}}\n"

我收到这条消息:

System.InvalidOperationException: 'Cannot convert object of type 'System.String' to type 'Test.ReturnValues+Response''

我不确定我做错了什么。我使用在线 json 转换器来获取我的 class。它似乎是正确的。

有什么建议吗?

使用 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("%s\n\t%s", 
   headline.attr("title"), headline.absUrl("href"));
 }`

我使用 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; }
    }
}

不过似乎可行。我做了一个快速比较,有很多不同之处。可能值得一试。