c#中将json数据转换为dictionary并取出某个元素显示

using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.Security.Cryptography;
using System.Text;
using LitJson;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Web.Script.Serialization;

namespace WpfApplication1
{
    /// 
    ///支付协议接口数据类,所有的API接口通信都依赖这个数据结构,
    /// 在调用接口之前先填充各个字段的值,然后进行接口通信,
    /// 这样设计的好处是可扩展性强,用户可随意对协议进行更改而不用重新设计数据结构,
    /// 还可以随意组合出不同的协议数据包,不用为每个协议设计一个数据包结构
    /// 
    public class SiWoData
    {
        [DataMember(Name = "Success")]
        public string Success { get; set; }
        [DataMember(Name = "Msg")]
        public string Msg { get; set; }
        [DataMember(Name = "OrderId")]
        public string OrderId { get; set; }
        [DataMember(Name = "Name")]
        public string Name { get; set; }

        [DataMember(Name = "Plate")]
        public string Plate { get; set; }
        [DataMember(Name = "InTime")]
        public string InTime { get; set; }
        [DataMember(Name = "OutTime")]
        public string OutTime { get; set; }
        [DataMember(Name = "UserType")]
       public string UserType { get; set; }

        [DataMember(Name = "ParkingSite")]
        public string ParkingSite { get; set; }
        [DataMember(Name = "ParkingTime")]
        public string ParkingTime { get; set; }
        [DataMember(Name = "ParkingFee")]
        public string ParkingFee { get; set; }
        [DataMember(Name = "ParkingActFee")]
        public string ParkingActFee { get; set; }







        public Dictionary dic = new Dictionary();
       
       


        private Dictionary JsonToDictionary(string jsonData)
        {

            //实例化JavaScriptSerializer类的新实例

            JavaScriptSerializer jss = new JavaScriptSerializer();

            try
            {

                //将指定的 JSON 字符串转换为 Dictionary 类型的对象

                return jss.Deserialize>(jsonData);

            }

            catch (Exception ex)
            {

                throw new Exception(ex.Message);

            }

        }
       

        public Dictionary SiWodatas(string serverUrl_g, string Data)
        {

            //Json数据

            string json =  Service.HttpGetConnectToServer(serverUrl_g, Data);
            dic = JsonToDictionary(json);//将Json数据转成dictionary格式
            return dic;
      }
    }
}

在主函数中

string serverUrl_g = "http://qs.cnpms.cn:6050/getparkingfee/?from=&plate=%E4%BA%ACA99999";
            //上传的数据 
                string Data = DingDanHao.Text;
                string response_g = Service.HttpGetConnectToServer(serverUrl_g, Data);            
                SiWoData result = new SiWoData();
                Dictionary dic=result.SiWodatas(serverUrl_g, Data);
                Label.Content = "车牌号:" + DingDanHao.Text + "\r\n" + "支付方式:微信支付" + "\r\n " +"驶入时间:"+ result.dic["InTime"] + "\r\n " +"驶出时间"+ result.dic["OutTime"];

result.dic["InTime"]
如此调用即可显示,即先将json数据转换为字典,返回字典,再实例化对象调用。

你可能感兴趣的:(c#中将json数据转换为dictionary并取出某个元素显示)