为了账号安全,请及时绑定邮箱和手机立即绑定

无法将 json 字符串反序列化为 ac# 类

无法将 json 字符串反序列化为 ac# 类

C#
回首忆惘然 2022-12-31 13:40:40
我有一个从 api 端点返回的 json 字符串,我正在尝试将其反序列化为 ac# 类。反序列化过程出错。传入的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; }    }}
查看完整描述

3 回答

?
撒科打诨

TA贡献1934条经验 获得超2个赞

您的 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.


查看完整回答
反对 回复 2022-12-31
?
莫回无

TA贡献1865条经验 获得超7个赞

您可以在线使用 JSON to Csharp 检查您的 C# 类

您需要更多步骤才能通过DeserializeemvTagData 为您的对象获取结果。

http://json2csharp.com/

在此处输入图像描述 使用您的 JSON 数据,您的课程应该是

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

}


查看完整回答
反对 回复 2022-12-31
?
喵喔喔

TA贡献1735条经验 获得超5个赞

您的 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"}



查看完整回答
反对 回复 2022-12-31
  • 3 回答
  • 0 关注
  • 134 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信