123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- using Ant.ORM.Table;
- using System.Data;
- namespace Ant.ORM
- {
- public class JsonHelper
- {
- /// <summary>
- /// 是否成功
- /// </summary>
- public bool Success
- {
- get
- {
- return count > 0;
- }
- }
- private string errorMsg = "";
- /// <summary>
- /// 错误提示信息
- /// </summary>
- public string ErrorMsg
- {
- get
- {
- return errorMsg;
- }
- set
- {
- errorMsg = value;
- }
- }
- private int count = 0;
- /// <summary>
- /// 总数
- /// </summary>
- public int Count
- {
- get
- {
- return count;
- }
- set
- {
- count = value;
- }
- }
- private List<string> arrData = new List<string>();
- #region 对象与对象之间分割符
- public void addItemOk()
- {
- arrData.Add("<br>");
- }
- #endregion
- #region 在数组里添加key,value
- public void addItem(string name, string value)
- {
- arrData.Add("\"" + name + "\":" + "\"" + value + "\"");
- }
- #endregion
- #region 返回组装好的json字符串
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("{");
- sb.Append("\"count\":\"" + count + "\",");
- sb.Append("\"error\":\"" + errorMsg + "\",");
- sb.Append("\"success\":\"" + (Success ? "true" : "") + "\",");
- sb.Append("\"data\":[");
- int index = 0;
- sb.Append("{");
- if (arrData.Count <= 0)
- {
- sb.Append("}]");
- }
- else
- {
- foreach (string val in arrData)
- {
- index++;
- if (val != "<br>")
- {
- sb.Append(val + ",");
- }
- else
- {
- sb = sb.Replace(",", "", sb.Length - 1, 1);
- sb.Append("},");
- if (index < arrData.Count)
- {
- sb.Append("{");
- }
- }
- }
- sb = sb.Replace(",", "", sb.Length - 1, 1);
- sb.Append("]");
- }
- sb.Append("}");
- return sb.ToString();
- }
- #endregion
- #region 为DataTable增加处理
- /// <summary>
- /// 从数据表中取数据填充,最终可输出json字符串
- /// </summary>
- public void Fill(MDataTable table)
- {
- if (table == null)
- {
- ErrorMsg = "table object is null";
- return;
- }
- Count = table.Rows.Count;
- for (int i = 0; i < table.Rows.Count; i++)
- {
- for (int j = 0; j < table.Columns.Count; j++)
- {
- addItem(table.Columns[j].ColumnName, Convert.ToString(table.Rows[i][j].Value));
- }
- addItemOk();
- }
- }
- /// <summary>
- /// 从Json字符串中反加载成数据表,默认字段结构都为string类型
- /// </summary>
- public MDataTable Load(string json)
- {
- MDataTable table = new MDataTable("loadFromJson");
- if (!string.IsNullOrEmpty(json) && json.StartsWith("{") && json.IndexOf(',') > -1 && json.EndsWith("}"))
- {
- List<string> items = new List<string>();
- if (json.Contains(":[{"))//多条数据
- {
- int start = json.IndexOf(":[{") + 2;
- json = json.Substring(start, json.LastIndexOf("}]") - start);
- string splitKey = "},{";
- start = json.IndexOf(splitKey);
- while (start > -1)
- {
- items.Add(json.Substring(0, start + 1));
- json = json.Remove(0, start + 2);
- start = json.IndexOf(splitKey);
- }
- }
- items.Add(json);
- if (items.Count > 0)
- {
- Dictionary<string, string> keyValueDic = Split(items[0]);
- if (keyValueDic != null && keyValueDic.Count > 0)
- {
- foreach (KeyValuePair<string, string> item in keyValueDic)
- {
- table.Columns.Add(item.Key, SqlDbType.NVarChar);
- }
- }
- foreach (string jsonItem in items)
- {
- keyValueDic = Split(jsonItem);
- if (keyValueDic != null && keyValueDic.Count > 0)
- {
- MDataRow row = table.NewRow();
- foreach (KeyValuePair<string, string> item in keyValueDic)
- {
- row.Set(item.Key, item.Value);
- }
- table.Rows.Add(row);
- }
- }
- }
- }
- return table;
- }
- public static Dictionary<string, string> Split(string json)
- {
- if (string.IsNullOrEmpty(json) || !json.StartsWith("{") || !json.EndsWith("}"))
- {
- return null;
- }
- json = json.TrimStart('{').TrimEnd('}');
- List<string> items = new List<string>();
- string splitKey = "\",\"";
- int index = json.IndexOf(splitKey);
- if (index == -1)
- {
- splitKey = "','";
- index = json.IndexOf(splitKey);
- if (index == -1)
- {
- splitKey = ",";
- index = json.IndexOf(splitKey);
- }
- }
- int startIndex = 0;
- string item = string.Empty;
- while (index > -1)
- {
- item = json.Substring(0, index + 1);
- if (item.Contains(":"))
- {
- items.Add(item);
- json = json.Remove(0, index + 2);
- startIndex = 0;
- }
- else
- {
- startIndex = index + 2;
- }
- index = json.IndexOf(splitKey, startIndex);
- }
- items.Add(json);
- Dictionary<string, string> keyValueDic = null;
- if (items.Count > 0)
- {
- keyValueDic = new Dictionary<string, string>();
- foreach (string keyValue in items)
- {
- index = keyValue.IndexOf(':');
- if (index > -1)
- {
- keyValueDic.Add(keyValue.Substring(0, index).Trim('\'', '\"'), keyValue.Substring(index + 1).Trim('\'', '\"'));
- }
- }
- }
- return keyValueDic;
- }
- #endregion
- }
- }
|