其他分享
首页 > 其他分享> > 写api接口的帮助类,排序字典和json、xml 之间的相互转换

写api接口的帮助类,排序字典和json、xml 之间的相互转换

作者:互联网

 

  1 /// <summary>
  2     /// api接口帮助类 字典和json、xml 之间的相互转换
  3     /// </summary>
  4     public class ZhongBangApiHelper
  5     {
  6         public ZhongBangApiHelper()
  7         {
  8 
  9         }
 10 
 11         /// <summary>
 12         /// 排序字典
 13         /// </summary>
 14         private readonly SortedDictionary<string, object> DataDic = new SortedDictionary<string, object>();
 15 
 16         /// <summary>
 17         /// 设置某个字段的值
 18         /// </summary>
 19         /// <param name="key">字段名</param>
 20         /// <param name="value">字段值</param>
 21         public void SetValue(string key, object value)
 22         {
 23             DataDic[key] = value;
 24         }
 25 
 26         /// <summary>
 27         /// 获取某个字段的值
 28         /// </summary>
 29         /// <param name="key">字段名</param>
 30         /// <returns></returns>
 31         public object GetValue(string key)
 32         {
 33             DataDic.TryGetValue(key, out object o);
 34             return o;
 35         }
 36 
 37         /// <summary>
 38         /// 判断某个字段是否已设置
 39         /// </summary>
 40         /// <param name="key"></param>
 41         /// <returns></returns>
 42         public bool IsSet(string key)
 43         {
 44             object o = null;
 45             DataDic.TryGetValue(key, out o);
 46             if (null != o)
 47                 return true;
 48             else
 49                 return false;
 50         }
 51 
 52 
 53         /// <summary>
 54         /// Dictionary序列化成Json
 55         /// </summary>
 56         /// <returns></returns>
 57         public string ToJson()
 58         {
 59             string jsonStr = JsonMapper.ToJson(DataDic);
 60             return jsonStr;
 61         }
 62 
 63         /// <summary>
 64         /// 将Dictionary转化成Xml
 65         /// </summary>
 66         /// <returns></returns>
 67         /// 
 68         public string ToXml()
 69         {
 70             //数据为空时不能转化为xml格式
 71             if (0 == DataDic.Count)
 72             {
 73                 throw new Exception("数据为空!");
 74             }
 75 
 76             string xml = "<xml>";
 77             foreach (KeyValuePair<string, object> pair in DataDic)
 78             {
 79                 //字段值不能为null,会影响后续流程
 80                 if (pair.Value == null)
 81                 {
 82                     throw new Exception("内部含有值为null的字段!");
 83                 }
 84 
 85                 if (pair.Value.GetType() == typeof(int))
 86                 {
 87                     xml += "<" + pair.Key + ">" + pair.Value + "</" + pair.Key + ">";
 88                 }
 89                 else if (pair.Value.GetType() == typeof(string))
 90                 {
 91                     xml += "<" + pair.Key + ">" + "<![CDATA[" + pair.Value + "]]></" + pair.Key + ">";
 92                 }
 93                 else//除了string和int类型不能含有其他数据类型
 94                 {
 95                     throw new Exception("字段数据类型错误!");
 96                 }
 97             }
 98             xml += "</xml>";
 99             return xml;
100         }
101 
102 
103         /// <summary>
104         /// 将xml转为 对象并返回对象内部的数据
105         /// </summary>
106         /// <param name="xml"></param>
107         /// <returns>经转换得到的Dictionary</returns>
108         /// 
109         public SortedDictionary<string, object> FromXml(string xml)
110         {
111             if (string.IsNullOrEmpty(xml))
112             {
113                 throw new Exception("将空的xml串转换为对象不合法!");
114             }
115 
116             XmlDocument xmlDoc = new XmlDocument();
117             xmlDoc.LoadXml(xml);
118             XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点<xml>
119             XmlNodeList nodes = xmlNode.ChildNodes;
120             foreach (XmlNode xn in nodes)
121             {
122                 XmlElement xe = (XmlElement)xn;
123                 DataDic[xe.Name] = xe.InnerText;//获取xml的键值对到 内部的数据中
124             }
125 
126             return DataDic;
127         }
128 
129         /// <summary>
130         /// Dictionary格式转化成url参数格式
131         /// </summary>
132         /// <returns>url格式串</returns>
133         /// 
134         public string ToUrl()
135         {
136             string buff = "";
137             foreach (KeyValuePair<string, object> pair in DataDic)
138             {
139                 if (pair.Value == null)
140                 {
141                     Log.Error(this.GetType().ToString(), "DataDic内部含有值为null的字段!");
142                     throw new Exception("DataDic内部含有值为null的字段!");
143                 }
144 
145                 if (pair.Key != "sign" && pair.Value.ToString() != "")
146                 {
147                     buff += pair.Key + "=" + pair.Value + "&";
148                 }
149             }
150             buff = buff.Trim('&');
151             return buff;
152         }
153 
154 
155         /// <summary>
156         /// 获取Dictionary
157         /// </summary>
158         /// <returns></returns>
159         public SortedDictionary<string, object> GetValues()
160         {
161             return DataDic;
162         }
163 
164         /// <summary>
165         /// 清空Dictionary
166         /// </summary>
167         public void ClearDic()
168         {
169             DataDic.Clear();
170         }
171 
172         /// <summary>
173         /// 字符串转为base64
174         /// </summary>
175         /// <param name="str"></param>
176         /// <returns></returns>
177         public string ToBase64(string str)
178         {
179             var byteStr = Encoding.UTF8.GetBytes(str);
180             var bs = Convert.ToBase64String(byteStr);
181             return bs.ToString();
182         }
183 
184     }

 

标签:xml,DataDic,return,string,json,api,pair,public
来源: https://www.cnblogs.com/heidashuaiGo/p/16454409.html