JsonHelper Json转xml,Json转DataSet,DataSet转Json
2017-05-18 13:51
316 查看
using System; using System.Collections.Generic; using System.Data; using System.Web.Script.Serialization; using System.Xml; namespace PowerEasy.Software.OrderSyncLibrary.Commom.Helper { /// <summary> /// JSON数据格式转换 /// </summary> public class JsonHelper { /// <summary> /// 将JSON解析成DataSet(只限标准的JSON数据) /// 例如:Json={t1:[{name:'数据name',type:'数据type'}]} 或 Json={t1:[{name:'数据name',type:'数据type'}],t2:[{id:'数据id',gx:'数据gx',val:'数据val'}]} /// </summary> /// <param name="json">Json字符串</param> /// <returns>DataSet</returns> public static DataSet JsonToDataSet(string json) { try { var ds = new DataSet(); var jss = new JavaScriptSerializer(); object obj = jss.DeserializeObject(json); var datajson = (Dictionary<string, object>) obj; foreach (var item in datajson) { var dt = new DataTable(item.Key); var rows = (object[]) item.Value; foreach (object row in rows) { var val = (Dictionary<string, object>) row; DataRow dr = dt.NewRow(); foreach (var sss in val) { if (!dt.Columns.Contains(sss.Key)) { dt.Columns.Add(sss.Key); dr[sss.Key] = sss.Value; } else dr[sss.Key] = sss.Value; } dt.Rows.Add(dr); } ds.Tables.Add(dt); } return ds; } catch { return null; } } /// <summary> /// 将DataSet转化成JSON数据 /// </summary> /// <param name="ds"></param> /// <returns></returns> public static string DataSetToJson(DataSet ds) { string json; try { if (ds.Tables.Count == 0) throw new Exception("DataSet中Tables为0"); json = "{"; for (int i = 0; i < ds.Tables.Count; i++) { json += "T" + (i + 1) + ":["; for (int j = 0; j < ds.Tables[i].Rows.Count; j++) { json += "{"; for (int k = 0; k < ds.Tables[i].Columns.Count; k++) { json += ds.Tables[i].Columns[k].ColumnName + ":'" + ds.Tables[i].Rows[j][k] + "'"; if (k != ds.Tables[i].Columns.Count - 1) json += ","; } json += "}"; if (j != ds.Tables[i].Rows.Count - 1) json += ","; } json += "]"; if (i != ds.Tables.Count - 1) json += ","; } json += "}"; } catch (Exception ex) { throw new Exception(ex.Message); } return json; } /// <summary> /// json字符串转换为Xml对象 /// </summary> /// <param name="sJson"></param> /// <returns></returns> public static XmlDocument JsonToXml(string sJson) { var serializer = new JavaScriptSerializer(); var dic = (Dictionary<string, object>) serializer.DeserializeObject(sJson); var doc = new XmlDocument(); XmlDeclaration xmlDec = doc.CreateXmlDeclaration("1.0", "gb2312", "yes"); doc.InsertBefore(xmlDec, doc.DocumentElement); XmlElement root = doc.CreateElement("root"); doc.AppendChild(root); foreach (var item in dic) { XmlElement element = doc.CreateElement(item.Key); KeyValue2Xml(element, item); root.AppendChild(element); } return doc; } private static void KeyValue2Xml(XmlElement node, KeyValuePair<string, object> source) { object kValue = source.Value; if (kValue.GetType() == typeof (Dictionary<string, object>)) { var dictionary = kValue as Dictionary<string, object>; if (dictionary != null) foreach (var item in dictionary) { if (node.OwnerDocument != null) { XmlElement element = node.OwnerDocument.CreateElement(item.Key); KeyValue2Xml(element, item); node.AppendChild(element); } } } else if (kValue.GetType() == typeof (object[])) { var o = kValue as object[]; if (o != null) foreach (object t in o) { if (node.OwnerDocument != null) { XmlElement xitem = node.OwnerDocument.CreateElement("Item"); var item = new KeyValuePair<string, object>("Item", t); KeyValue2Xml(xitem, item); node.AppendChild(xitem); } } } else { if (node.OwnerDocument != null) { XmlText text = node.OwnerDocument.CreateTextNode(kValue.ToString()); node.AppendChild(text); } } } } }
相关文章推荐
- JsonHelper Json转xml,Json转DataSet,DataSet转Json
- JsonHelper Json转xml,Json转DataSet,DataSet转Json
- JsonHelper developed by using Newtonsoft.Json.NET, Deserialize to <T> object , XmlToJson/JsonToXml, QuoteName by using JToken Path.
- spring mvc 多视图 freemarker json xml 以及自定义dataset
- .Net 2.0: Entity as DTO vs Dataset as DTO / Xml Serialization vs JSON Serialization
- WebService返回DataSet,Xml,Json
- 在将DataSet转成XML时遇到一个非法字符的问题
- Spring MVC 多视图协商配置(json、xml、freemarker)
- java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException
- DataSet(DataTable)转化为json格式
- Android基础之XML和JSON数据解析
- 将从数据库得到的信息分别以HTML,XML,Json的形式输出
- C# XML操作 代码大全(读XML,写XML,更新,删除节点,与dataset结合等)
- XMLHelper
- 来数一数XML解析成为Dataset数据
- 将XML形式的数据转换成Json格式
- 再谈JSON/XML
- Xml Helper
- 【unity】将场景导出XML或JSON或二进制并且解析还原场景(补充)
- DataSet和XML