【问题标题】:How to deserialize TcpClient response to json如何反序列化 TcpClient 对 json 的响应
【发布时间】:2021-09-20 19:12:17
【问题描述】:

我正忙于将应用程序与使用 tcp 连接的财务设备集成。当我向它发送数据并得到响应并尝试使用 json 反序列化它时,它不起作用。这是我的 tcp 客户端和我尝试使用 json 反序列化的响应数据。文档说响应是 json 格式,但似乎存在一些格式问题。如果您能告诉我反序列化响应的最佳方法,将不胜感激。

TCP 客户端

        private String Send(byte[] hexData)
    {
        byte[] data = hexData; // Encoding.ASCII.GetBytes(ConvertHex(Encoding.ASCII.GetString(hexData)));
        //System.Diagnostics.Debug.WriteLine(ConvertHex(Encoding.ASCII.GetString(hexData)));
             String response = String.Empty ;
                  
                using (TcpClient client = new TcpClient())
                {
                    client.Connect("192.168.8.98", 8888);
                    client.GetStream().Write(data, 0, data.Length);
                    System.Diagnostics.Debug.WriteLine("Data Sent");
                    byte[] receiveData = new byte[100024];
                    String responseData = String.Empty;
                    Int32 bytes = client.GetStream().Read(receiveData, 0, receiveData.Length);
                    response = Encoding.ASCII.GetString(receiveData, 0, bytes);
                }

                System.Diagnostics.Debug.WriteLine(response);
                               
                var Result = JsonConvert.DeserializeObject<ESD.GetStatusResponse>(response);
                
            return response;
    }

如您在上面看到的,我正在尝试反序列化的响应数据。在我得到错误的反序列化部分。 Get StatusReponse 类与文档相匹配,我可以在下面的数据中看到一些属性。

\u001a]\u0002\0\0\u0006?{\n\t"HardwareVersion":\t"2.0",\n\t"SoftwareVersion":\t"1.042",\n\t"SerialNumber ":\t"100100003505",\n\t"制造":\t"浪潮",\n\t"型号":\t"IS-100",\n\t"TPIN":\t"1001749833 ",\n\t"纳税人姓名":\t"ZAMBIA SHOE COMPANY PLC",\n\t"地址":\t"PLOT NO. 6007, MUKWA ROAD, INDUSTRIAL AREA",\n\t"ESDTime": \t"20210918180305",\n\t"终端 ID":\t"010300000051",\n\t"isInitialized":\ttrue,\n\t"isLocked":\tfalse,\n\t"MonitoringInfo": \t{\n\t\t"OfflineNum":\t0,\n\t\t"SingleAmount":\t0,\n\t\t"MonthlyInvoiceQuantity":\t0,\n\t\t"MonthlyCreditNoteAmount ":\t0,\n\t\t"InvoiceHoldingQuantity":\t20,\n\t\t"RemainThreshold":\t20,\n\t\t"MonthlyCreditNoteNum":\t0\n\t},\ n\t"TaxInfo":\t[{\n\t\t\t"TaxType":\t"01",\n\t\t\t"Category":\t[{\n\t\ t\t\t\t"CategoryId":\t1,\n\t\t\t\t\t"TaxLabel":\t"A",\n\t\t\t\t\t"TaxName ":\t"标准评级",\n\t\t\t\t\t"TaxRate":\t0.16,\n\t\t\t\t\t"EffectiveDate":\t"2018 -01-10",\n\t\t\t\t\t"过期日期":\t"2031-01-01"\n\t\t\t\t}, {\n\t\t \t\t\t"CategoryId":\t2,\n\t \t\t\t\t"TaxLabel":\t"B",\n\t\t\t\t\t"TaxName":\t"MTVs",\n\t\t\t\t \t"TaxRate":\t0.16,\n\t\t\t\t\t"EffectiveDate":\t"2018-01-10",\n\t\t\t\t\t" ExpiredDate":\t"2031-01-01"\n\t\t\t\t}, {\n\t\t\t\t\t"CategoryId":\t3,\n\t\t \t\t\t"TaxLabel":\t"C1",\n\t\t\t\t\t"TaxName":\t"EXPORT",\n\t\t\t\t\t "TaxRate":\t0,\n\t\t\t\t\t"EffectiveDate":\t"2018-01-10",\n\t\t\t\t\t"ExpiredDate":\ t"2031-01-01"\n\t\t\t\t}, {\n\t\t\t\t\t"CategoryId":\t4,\n\t\t\t\t \t"TaxLabel":\t"C2",\n\t\t\t\t\t"TaxName":\t"PRIVILEGED PERSONS",\n\t\t\t\t\t"TaxRate" :\t0,\n\t\t\t\t\t"生效日期":\t"2018-01-10",\n\t\t\t\t\t"过期日期":\t"2031 -01-01"\n\t\t\t\t}, {\n\t\t\t\t\t"CategoryId":\t5,\n\t\t\t\t\t" TaxLabel":\t"C3",\n\t\t\t\t\t"TaxName":\t"ZERO RATED",\n\t\t\t\t\t"TaxRate":\t0 ,\n\t\t\t\t\t"生效日期":\t"2018-01-10",\n\t\t\t\t\t"过期日期":\t"2031-01- 01"\n\t\t\t\t}, {\n\t\t\t\t\t"CategoryId":\t6,\n\t\t\t\t\t"TaxLabel": \t"D",\n\t\t\t\t\t"TaxName":\t"EXEMPT",\n\t\t\t\t\t"TaxRate":\t0,\n\ t\t\t\t\t"生效日期":\t"2018-01-1 0",\n\t\t\t\t\t"过期日期":\t"2031-01-01"\n\t\t\t\t}]\n\t\t}]\n }\0Y:

这里是 GetStatusReponse 类

        public class GetStatusResponse
    {
        public string HardwareVersion { get; set; }
        public string SoftwareVersion { get; set; }
        public string SerialNumber { get; set; }
        public string Manufacture { get; set; }
        public string Model { get; set; }
        public string TPIN { get; set; }
        public string TaxpayerName { get; set; }
        public string Addreess { get; set; }
        public string ESDTime { get; set; }
        public string TerminalID { get; set; }
        public bool isInitialized { get; set; }
        public bool isLocked { get; set; }
        public Monitoringinfo MonitoringInfo { get; set; }
        public Taxinfo[] TaxInfo { get; set; }
    }

【问题讨论】:

    标签: c# json tcpclient


    【解决方案1】:

    我找到了解决问题的方法。我所做的只是清理响应然后反序列化它。

            public static string CleanResponse(string value)
        {
            int firstbrace = value.IndexOf('{');
            string res = value.Substring(firstbrace);
            int lastbrace = res.LastIndexOf('}');
            res = res.Substring(0, lastbrace + 1);
            return res; 
        }
    

    这就是我对响应所做的事情

    response = Helper.CleanResponse(response);
    
    var Result = JsonConvert.DeserializeObject<GetStatusResponse>(response);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      相关资源
      最近更新 更多