在C#中解析表单数据的响应

时间:2017-12-12 15:47:19

标签: c# json multipartform-data

    type=CREATE
resource=transaction
number_of_attempts=1
data={"id":"11e7df5131dd9820bf774044","payment_method":"cc","account_vault_id":null,"recurring_id":null,"first_six":"411111","last_four":"1111","account_holder_name":" MALCOLM BLACK","transaction_amount":"1.00","description":null,"transaction_code":null,"avs":null,"batch":"2","order_num":"798279608211","verbiage":"APPROVAL","transaction_settlement_status":null,"effective_date":null,"routing":null,"return_date":null,"created_ts":1513092547,"modified_ts":1513092547,"transaction_api_id":null,"terms_agree":null,"notification_email_address":null,"notification_email_sent":true,"response_message":null,"auth_amount":"1.00","auth_code":"df5132","status_id":101,"type_id":20,"location_id":"11e7c499daaff1f0a7507909","reason_code_id":1000,"contact_id":"11e7c619b69d209ab41b24dc","billing_zip":"","billing_street":null,"product_transaction_id":"11e7c499db893b0489bb3739","tax":"0.000","customer_ip":null,"customer_id":null,"po_number":null,"avs_enhanced":"V","cvv_response":"N","billing_phone":null,"billing_city":null,"billing_state":null,"clerk_number":null,"tip_amount":"0.00","created_user_id":"11e7c499dac79026a4e740f1","modified_user_id":"11e7c499dac79026a4e740f1","settle_date":null,"charge_back_date":null,"void_date":null,"account_type":"visa","is_recurring":false,"is_accountvault":false,"transaction_c1":null,"transaction_c2":null,"transaction_c3":null,"additional_amounts":[],"terminal_serial_number":null,"entry_mode_id":"K","terminal_id":null,"quick_invoice_id":null,"emv_receipt_data":null}

这是我在C#中使用

收到的表格数据
string data=Request.Form("data");

现在嵌套数据如何获取data对象的值。使用JsonConvert.Deserialize在json中解析它会引发异常。

最初的回应是:

type=CREATE&resource=transaction&number_of_attempts=1&data=%7b%22id%22%3a%2211e7df5131dd9820bf774044%22%2c%22payment_method%22%3a%22cc%22%2c%22account_vault_id%22%3anull%2c%22recurring_id%22%3anull%2c%22first_six%22%3a%22411111%22%2c%22last_four%22%3a%221111%22%2c%22account_holder_name%22%3a%22+MALCOLM+BLACK%22%2c%22transaction_amount%22%3a%221.00%22%2c%22description%22%3anull%2c%22transaction_code%22%3anull%2c%22avs%22%3anull%2c%22batch%22%3a%222%22%2c%22order_num%22%3a%22798279608211%22%2c%22verbiage%22%3a%22APPROVAL%22%2c%22transaction_settlement_status%22%3anull%2c%22effective_date%22%3anull%2c%22routing%22%3anull%2c%22return_date%22%3anull%2c%22created_ts%22%3a1513092547%2c%22modified_ts%22%3a1513092547%2c%22transaction_api_id%22%3anull%2c%22terms_agree%22%3anull%2c%22notification_email_address%22%3anull%2c%22notification_email_sent%22%3atrue%2c%22response_message%22%3anull%2c%22auth_amount%22%3a%221.00%22%2c%22auth_code%22%3a%22df5132%22%2c%22status_id%22%3a101%2c%22type_id%22%3a20%2c%22location_id%22%3a%2211e7c499daaff1f0a7507909%22%2c%22reason_code_id%22%3a1000%2c%22contact_id%22%3a%2211e7c619b69d209ab41b24dc%22%2c%22billing_zip%22%3a%22%22%2c%22billing_street%22%3anull%2c%22product_transaction_id%22%3a%2211e7c499db893b0489bb3739%22%2c%22tax%22%3a%220.000%22%2c%22customer_ip%22%3anull%2c%22customer_id%22%3anull%2c%22po_number%22%3anull%2c%22avs_enhanced%22%3a%22V%22%2c%22cvv_response%22%3a%22N%22%2c%22billing_phone%22%3anull%2c%22billing_city%22%3anull%2c%22billing_state%22%3anull%2c%22clerk_number%22%3anull%2c%22tip_amount%22%3a%220.00%22%2c%22created_user_id%22%3a%2211e7c499dac79026a4e740f1%22%2c%22modified_user_id%22%3a%2211e7c499dac79026a4e740f1%22%2c%22settle_date%22%3anull%2c%22charge_back_date%22%3anull%2c%22void_date%22%3anull%2c%22account_type%22%3a%22visa%22%2c%22is_recurring%22%3afalse%2c%22is_accountvault%22%3afalse%2c%22transaction_c1%22%3anull%2c%22transaction_c2%22%3anull%2c%22transaction_c3%22%3anull%2c%22additional_amounts%22%3a%5b%5d%2c%22terminal_serial_number%22%3anull%2c%22entry_mode_id%22%3a%22K%22%2c%22terminal_id%22%3anull%2c%22quick_invoice_id%22%3anull%2c%22emv_receipt_data%22%3anull%7d

更新:

try
                {

                    string data = @Request.Form.Get("data");


    //now data=
 //{"id":"11e7df5131dd9820bf774044","payment_method":"cc","account_vault_id":n//ull.....}
                    //data = '"' + data + '"';
                    var stringfy = JsonConvert.DeserializeObject(data);

                    File.WriteAllText(path, " File Data : " + Request.Form +
                    Environment.NewLine + "     --------------------Total Value 0 " + Request.Form.Keys[0].ToString()
                    + Environment.NewLine + "     --------------------Total Value 1 " + Request.Form.Keys[1].ToString()
                    + Environment.NewLine + "     --------------------Total Value 2 " + Request.Form.Keys[2].ToString()
                     + Environment.NewLine + "     --------------------Total Value 3 " + data);
                }
                catch (Exception ex)
                {
                    File.WriteAllText(path, "Exception :1 "+ex.Message.ToString());
                }

代码部署在服务器上,因此将异常写入文件,但Deserialize语句工作后没有代码,也没有创建文件。

1 个答案:

答案 0 :(得分:0)

有一条捷径。您可以将此网站用作帮助 - http://json2csharp.com/

您需要做的就是通过此字符串{" id":" 11e7d ...}然后点击生成。

// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var data = Welcome.FromJson(jsonString);
//

namespace QuickType
{
    using System;
    using System.Net;
    using System.Collections.Generic;
    using Newtonsoft.Json;

public partial class Welcome
{
    [JsonProperty("account_holder_name")]
    public string AccountHolderName { get; set; }

    [JsonProperty("account_type")]
    public string AccountType { get; set; }

    [JsonProperty("account_vault_id")]
    public object AccountVaultId { get; set; }

    [JsonProperty("additional_amounts")]
    public object[] AdditionalAmounts { get; set; }

    [JsonProperty("auth_amount")]
    public string AuthAmount { get; set; }

    [JsonProperty("auth_code")]
    public string AuthCode { get; set; }

    [JsonProperty("avs")]
    public object Avs { get; set; }

    [JsonProperty("avs_enhanced")]
    public string AvsEnhanced { get; set; }

    [JsonProperty("batch")]
    public string Batch { get; set; }

    [JsonProperty("billing_city")]
    public object BillingCity { get; set; }

    [JsonProperty("billing_phone")]
    public object BillingPhone { get; set; }

    [JsonProperty("billing_state")]
    public object BillingState { get; set; }

    [JsonProperty("billing_street")]
    public object BillingStreet { get; set; }

    [JsonProperty("billing_zip")]
    public string BillingZip { get; set; }

    [JsonProperty("charge_back_date")]
    public object ChargeBackDate { get; set; }

    [JsonProperty("clerk_number")]
    public object ClerkNumber { get; set; }

    [JsonProperty("contact_id")]
    public string ContactId { get; set; }

    [JsonProperty("created_ts")]
    public long CreatedTs { get; set; }

    [JsonProperty("created_user_id")]
    public string CreatedUserId { get; set; }

    [JsonProperty("customer_id")]
    public object CustomerId { get; set; }

    [JsonProperty("customer_ip")]
    public object CustomerIp { get; set; }

    [JsonProperty("cvv_response")]
    public string CvvResponse { get; set; }

    [JsonProperty("description")]
    public object Description { get; set; }

    [JsonProperty("effective_date")]
    public object EffectiveDate { get; set; }

    [JsonProperty("emv_receipt_data")]
    public object EmvReceiptData { get; set; }

    [JsonProperty("entry_mode_id")]
    public string EntryModeId { get; set; }

    [JsonProperty("first_six")]
    public string FirstSix { get; set; }

    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("is_accountvault")]
    public bool IsAccountvault { get; set; }

    [JsonProperty("is_recurring")]
    public bool IsRecurring { get; set; }

    [JsonProperty("last_four")]
    public string LastFour { get; set; }

    [JsonProperty("location_id")]
    public string LocationId { get; set; }

    [JsonProperty("modified_ts")]
    public long ModifiedTs { get; set; }

    [JsonProperty("modified_user_id")]
    public string ModifiedUserId { get; set; }

    [JsonProperty("notification_email_address")]
    public object NotificationEmailAddress { get; set; }

    [JsonProperty("notification_email_sent")]
    public bool NotificationEmailSent { get; set; }

    [JsonProperty("order_num")]
    public string OrderNum { get; set; }

    [JsonProperty("payment_method")]
    public string PaymentMethod { get; set; }

    [JsonProperty("po_number")]
    public object PoNumber { get; set; }

    [JsonProperty("product_transaction_id")]
    public string ProductTransactionId { get; set; }

    [JsonProperty("quick_invoice_id")]
    public object QuickInvoiceId { get; set; }

    [JsonProperty("reason_code_id")]
    public long ReasonCodeId { get; set; }

    [JsonProperty("recurring_id")]
    public object RecurringId { get; set; }

    [JsonProperty("response_message")]
    public object ResponseMessage { get; set; }

    [JsonProperty("return_date")]
    public object ReturnDate { get; set; }

    [JsonProperty("routing")]
    public object Routing { get; set; }

    [JsonProperty("settle_date")]
    public object SettleDate { get; set; }

    [JsonProperty("status_id")]
    public long StatusId { get; set; }

    [JsonProperty("tax")]
    public string Tax { get; set; }

    [JsonProperty("terminal_id")]
    public object TerminalId { get; set; }

    [JsonProperty("terminal_serial_number")]
    public object TerminalSerialNumber { get; set; }

    [JsonProperty("terms_agree")]
    public object TermsAgree { get; set; }

    [JsonProperty("tip_amount")]
    public string TipAmount { get; set; }

    [JsonProperty("transaction_amount")]
    public string TransactionAmount { get; set; }

    [JsonProperty("transaction_api_id")]
    public object TransactionApiId { get; set; }

    [JsonProperty("transaction_c1")]
    public object TransactionC1 { get; set; }

    [JsonProperty("transaction_c2")]
    public object TransactionC2 { get; set; }

    [JsonProperty("transaction_c3")]
    public object TransactionC3 { get; set; }

    [JsonProperty("transaction_code")]
    public object TransactionCode { get; set; }

    [JsonProperty("transaction_settlement_status")]
    public object TransactionSettlementStatus { get; set; }

    [JsonProperty("type_id")]
    public long TypeId { get; set; }

    [JsonProperty("verbiage")]
    public string Verbiage { get; set; }

    [JsonProperty("void_date")]
    public object VoidDate { get; set; }
}

public partial class Welcome
{
    public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, Converter.Settings);
}

public static class Serialize
{
    public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, Converter.Settings);
}

public class Converter
{
    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
        DateParseHandling = DateParseHandling.None,
    };
}

}