当前位置: 首页 > 知识库问答 >
问题:

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

顾赞
2023-03-14

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

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

我不确定我做错了什么。我使用一个在线json转换器来获取我的类。看来是正确的。

有什么建议吗?

共有1个答案

夹谷飞龙
2023-03-14

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

不过似乎很管用。我做了一个快速比较,有相当多的差异。可能值得一看。

 类似资料:
  • 问题内容: 我正在使用CURL发送请求。响应数据类型为。如何解析此数据并将其插入数据库? JSON输出: 问题答案: 如果您的变量是字符串json之类的,则必须使用function将其解析为 对象 或 数组 : 输出值 现在,您可以将变量作为数组使用: 参考文献: json_decode -PHP手册

  • 问题内容: 我已经编写了REST API服务,该服务要求所有响应均为JSON。但是,当Go HTTP请求解析器遇到错误时,它将返回400作为纯文本响应,而无需调用我的处理程序。例: 请注意无效的授权标头。当然,400是正确的响应,但是当然是文本/纯文本。有什么方法可以配置Go http解析器以使用自定义错误响应媒体类型和主体? 问题答案: 你不能 您可以在net / http源中找到它,只有在请求

  • 我得到了一个配置好的jackson的ObjectMapper实例,其中应用了一些模块、反序列化器和配置。 我还有一个“平面”json,这意味着要么没有内部节点,要么ObjectMapper能够将内部节点解析为单个对象。 我想将给定的json解析为映射 这就像用解析pojo,然后使用反射收集pojo字段。但是我想避免提取pojo的类,避免使用反射并获取结果Map只存在于json属性中。

  • 我希望有人能帮我解决这个问题。 我想用WebClient创建一个Rest客户端,从API中检索响应。所以我创建了我的Spring项目,添加了webflux、lombok和h2。我还创建了一个DTO类“CashAccount”和以下方法: 当我使用“.bodyToMono(String.class)”时,所有的功能都很好,我收到了结果: 相反,当我使用“.bodyToMono(cashcount.c

  • 问题内容: 我收到此响应的结果是对服务器的GET请求 我只想从上述json响应中提取的值。 我正在使用此代码来获得此响应 我的问题是,我该如何解析并获取only 标签的值。谢谢 问题答案: 您可以解析当前的json字符串以从中获取它:

  • 我最近读了很多关于DNS的书,甚至在堆栈溢出上搜索信息,但是我似乎找不到这个。如果一路上有错误,请纠正我。 假设我想去example.com.我理解DNS工作方式的方式是 > 它在本地客户机上查找缓存的记录。如果缓存中有记录,一切都很好。如果没有,则移动到步骤2。 向Internet服务提供商(ISP)提供的DNS解析程序请求域名。 如果DNS解析器具有域名记录缓存,则返回IP地址,一切正常。如果