我有一个从api端点返回的json字符串,我正在尝试将其反序列化为c#类。反序列化过程错误。
传入的json字符串:
{"amount":"3.00","resptext":"Approval","cvvresp":"P","respcode":"000","avsresp":"","merchid":"800000000800","token":"9478837814450119","authcode":"PPS306","respproc":"RPCT","emvTagData":"{\"TVR\":\"0200008000\",\"PIN\":\"None\",\"Signature\":\"true\",\"Mode\":\"Issuer\",\"ARC\":\"Z3\",\"TSI\":\"E800\",\"Application Preferred Name\":\"CREDITO DE VISA\",\"AID\":\"A0000000031010\",\"IAD\":\"06010A03A00000\",\"Entry method\":\"Chip Read\",\"Application Label\":\"VISA CREDIT\"}","retref":"116390139157","respstat":"A","account":"47XXXXXXXXXX0119"}
c#类:
public class AuthCardResponse
{
public string token { get; set; }
public string expiry { get; set; }
public string name { get; set; }
public string batchid { get; set; }
public string retref { get; set; }
public string avsresp { get; set; }
public string respproc { get; set; }
public string amount { get; set; }
public string resptext { get; set; }
public string authcode { get; set; }
public string respcode { get; set; }
public string merchid { get; set; }
public string cvvresp { get; set; }
public string respstat { get; set; }
public string account { get; set; }
public string bintype { get; set; }
public string entrymode{get;set;}
public string commcard { get; set; }
//public string emvTagData { get; set; }
public EmvTagData emvTagData { get; set; }
}
public class EmvTagData
{
public string TVR { get; set; }
public string PIN { get; set; }
public string Signature { get; set; }
public string Mode { get; set; }
public string TSI { get; set; }
public string AID { get; set; }
public string ARC { get; set; }
public string IAD { get; set; }
[JsonProperty("Entry method")]
public string Entrymethod { get; set; }
[JsonProperty("Application Label")]
public string ApplicationLabel { get; set; }
[JsonProperty("Application Preferred Name")]
public string ApplicationPreferredName { get; set; }
}
}
反序列化的代码:
AuthCardResponse _authCardResponse = JsonConvert.DeserializeObject<AuthCardResponse>(authResultJson );
错误:
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
我无法弄清楚错误是什么。我已经尝试过在线json解析器,他们已经能够解析字符串而没有任何问题。
任何帮助表示赞赏。
谢谢NH
您的emvTagData是一个文字字符串,但您尝试将其反序列化为复杂对象。
"emvTagData":"{\"TVR\":\"0200008000\",\"PIN\":\"None\",\"Signature\":\"true\",\"Mode\":\"Issuer\",\"ARC\":\"Z3\",\"TSI\":\"E800\",\"Application Preferred Name\":\"CREDITO DE VISA\",\"AID\":\"A0000000031010\",\"IAD\":\"06010A03A00000\",\"Entry method\":\"Chip Read\",\"Application Label\":\"VISA CREDIT\"}"
看到你已经把它作为string
类型在你的AuthCardResponse
类中注释了,我希望你已经发现你可以将它反序列化为string
。
您可以在线使用JSON到Csharp来检查您的C#类
您需要更多步骤才能将Deserialize
emvTagData的结果输出到您的对象。
public class RootObject
{
public string amount { get; set; }
public string resptext { get; set; }
public string cvvresp { get; set; }
public string respcode { get; set; }
public string avsresp { get; set; }
public string merchid { get; set; }
public string token { get; set; }
public string authcode { get; set; }
public string respproc { get; set; }
public string emvTagData { get; set; }
public string retref { get; set; }
public string respstat { get; set; }
public string account { get; set; }
}
你的json对Newtonsoft.Json有错误的语法。您的json中的对象(EmvTagData)应如下所示:
"emvTagData": {
"TVR": "0200008000",
"PIN": "None",
"Signature": "true",
"Mode": "Issuer",
"ARC": "Z3",
"TSI": "E800",
"Application Preferred Name": "CREDITO DE VISA",
"AID": "A0000000031010",
"IAD": "06010A03A00000",
"Entry method": "Chip Read",
"Application Label": "VISA CREDIT"}
在主括号之前和之后没有\“和”。
整个json:
{"amount":"3.00","resptext":"Approval","cvvresp":"P","respcode":"000","avsresp":"","merchid":"800000000800","token":"9478837814450119","authcode":"PPS306","respproc":"RPCT","emvTagData":{"TVR":"0200008000","PIN":"None","Signature":"true","Mode":"Issuer","ARC":"Z3","TSI":"E800","Application Preferred Name":"CREDITO DE VISA","AID":"A0000000031010","IAD":"06010A03A00000","Entry method":"Chip Read","Application Label":"VISA CREDIT"},"retref":"116390139157","respstat":"A","account":"47XXXXXXXXXX0119"}