C# C语言中表单数据的解析响应#
这是我正在使用C#接收的表单数据 现在数据已嵌套,如何获取C# C语言中表单数据的解析响应#,c#,json,multipartform-data,C#,Json,Multipartform Data,这是我正在使用C#接收的表单数据 现在数据已嵌套,如何获取dataobject的值。在json中使用JsonConvert.Deserialize解析它会引发异常 最初的答复是: string data=Request.Form("data"); 更新: type=CREATE&resource=transaction&number_of_attempts=1&data=%7b%22id%22%3a%2211e7df5131dd9820bf774044%22%2c%22
data
object的值。在json中使用JsonConvert.Deserialize
解析它会引发异常
最初的答复是:
string data=Request.Form("data");
更新:
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
代码部署在服务器上,所以将异常写入文件中,但
反序列化
语句之后的代码不起作用,也不会创建任何文件 有一条捷径。您可以使用此网站作为帮助者-
您需要做的就是通过这个字符串{“id”:“11e7d…},然后单击generate
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());
}
//若要解析此JSON数据,请添加NuGet“Newtonsoft.JSON”,然后执行以下操作:
//
//使用QuickType;
//
//var data=Welcome.FromJson(jsonString);
//
命名空间快速类型
{
使用制度;
Net系统;
使用System.Collections.Generic;
使用Newtonsoft.Json;
欢迎参加部分公开课
{
[JsonProperty(“账户持有人名称”)]
公共字符串AccountHolderName{get;set;}
[JsonProperty(“账户类型”)]
公共字符串AccountType{get;set;}
[JsonProperty(“账户库id”)]
公共对象AccountVaultId{get;set;}
[JsonProperty(“额外金额”)]
公共对象[]附加数量{get;set;}
[JsonProperty(“授权金额”)]
公共字符串AuthAmount{get;set;}
[JsonProperty(“认证代码”)]
公共字符串AuthCode{get;set;}
[JsonProperty(“avs”)]
公共对象Avs{get;set;}
[JsonProperty(“avs_增强型”)]
公共字符串AvsEnhanced{get;set;}
[JsonProperty(“批次”)]
公共字符串批处理{get;set;}
[JsonProperty(“计费城市”)]
公共对象BillingCity{get;set;}
[JsonProperty(“计费电话”)]
公共对象计费电话{get;set;}
[JsonProperty(“账单状态”)]
公共对象BillingState{get;set;}
[JsonProperty(“billing_street”)]
公共对象BillingStreet{get;set;}
[JsonProperty(“账单”)]
公共字符串BillingZip{get;set;}
[JsonProperty(“押回日期”)]
公共对象ChargeBackDate{get;set;}
[JsonProperty(“职员编号”)]
公共对象ClerkNumber{get;set;}
[JsonProperty(“联系人id”)]
公共字符串ContactId{get;set;}
[JsonProperty(“已创建”)]
public long CreatedTs{get;set;}
[JsonProperty(“创建的用户id”)]
公共字符串CreatedUserId{get;set;}
[JsonProperty(“客户id”)]
公共对象CustomerId{get;set;}
[JsonProperty(“客户知识产权”)]
公共对象CustomerIp{get;set;}
[JsonProperty(“cvv_响应”)]
公共字符串CvvResponse{get;set;}
[JsonProperty(“描述”)]
公共对象描述{get;set;}
[JsonProperty(“生效日期”)]
公共对象有效日期{get;set;}
[JsonProperty(“emv_收据_数据”)]
公共对象EmvReceiptData{get;set;}
[JsonProperty(“入口模式id”)]
公共字符串EntryModeId{get;set;}
[JsonProperty(“前六名”)]
公共字符串FirstSix{get;set;}
[JsonProperty(“id”)]
公共字符串Id{get;set;}
[JsonProperty(“is_accountvault”)]
公共bool IsAccountvault{get;set;}
[JsonProperty(“是重复出现的”)]
公共bool正在加密{get;set;}
[JsonProperty(“最后四个”)]
公共字符串LastFour{get;set;}
[JsonProperty(“位置标识”)]
公共字符串LocationId{get;set;}
[JsonProperty(“修改的”)]
public long ModifiedTs{get;set;}
[JsonProperty(“修改的用户id”)]
公共字符串ModifiedUserId{get;set;}
[JsonProperty(“通知电子邮件地址”)]
公共对象通知电子邮件地址{get;set;}
[JsonProperty(“发送通知和电子邮件”)]
public bool NotificationEmailSent{get;set;}
[JsonProperty(“订单数量”)]
公共字符串OrderNum{get;set;}
[JsonProperty(“付款方式”)]
公共字符串PaymentMethod{get;set;}
[JsonProperty(“po_编号”)]
公共对象PoNumber{get;set;}
[JsonProperty(“产品交易id”)]
公共字符串ProductTransactionId{get;set;}
[JsonProperty(“快速发票id”)]
公共对象QuickInvoiceId{get;set;}
[JsonProperty(“原因代码id”)]
公共长代码ID{get;set;}
[JsonProperty(“重复出现的id”)]
公共对象递归ID{get;set;}
[JsonProperty(“响应消息”)]
公共对象响应消息{get;set;}
[JsonProperty(“归还日期”)]
公共对象返回日期{get;set;}
[JsonProperty(“路由”)]
公共对象路由{get;set;}
[JsonProperty(“结算日期”)]
公共对象结算日期{get;set;}
[JsonProperty(“状态id”)]
公共长状态ID{get;set;}
[JsonProperty(“税”)]
公共字符串Tax{get;set;}
[JsonProperty(“终端id”)]
公共对象TerminalId{get;set;}
[JsonProperty(“终端序列号”)]
公共对象TerminalSerialNumber{get;set;}
[JsonProperty(“双方同意的条款”)]
公共对象TermsAgree{get;set;}
[JsonProperty(“小费金额”)]
公共字符串TipAmount{get;set;}
[JsonProperty(“交易金额”)]
公共字符串TransactionAmount{get;set;}
[JsonProperty(“交易api id”)]
公共对象TransactionApiId{get;set;}
[JsonProperty(“交易_c1”)]
公共对象事务C1{get;set;}
[JsonProperty(“交易”)]
公共对象事务处理C2{get;set;}
[JsonProperty(“交易_c3”)]
公共对象事务处理C3{get;set;}
[JsonProperty(“交易代码”)]
公共对象事务代码{get;set;}
[JsonProperty(“交易结算状态”)]
公共对象TransactionSettlementStatus{get;set;}
[JsonProperty(“type_id”)]
公共长类型ID{get;set;}
[JsonProperty(“措辞”)]
公共字符串冗余{get;set;}
[JsonProperty(“作废日期”)]
公共对象VoidDate{get;set;}
}
公共部分c
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());
}
// 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,
};
}