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
{
///
/// 是否成功
///
public bool Success
{
get
{
return count > 0;
}
}
private string errorMsg = "";
///
/// 错误提示信息
///
public string ErrorMsg
{
get
{
return errorMsg;
}
set
{
errorMsg = value;
}
}
private int count = 0;
///
/// 总数
///
public int Count
{
get
{
return count;
}
set
{
count = value;
}
}
private List arrData = new List();
#region 对象与对象之间分割符
public void addItemOk()
{
arrData.Add("
");
}
#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 != "
")
{
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增加处理
///
/// 从数据表中取数据填充,最终可输出json字符串
///
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();
}
}
///
/// 从Json字符串中反加载成数据表,默认字段结构都为string类型
///
public MDataTable Load(string json)
{
MDataTable table = new MDataTable("loadFromJson");
if (!string.IsNullOrEmpty(json) && json.StartsWith("{") && json.IndexOf(',') > -1 && json.EndsWith("}"))
{
List items = new List();
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 keyValueDic = Split(items[0]);
if (keyValueDic != null && keyValueDic.Count > 0)
{
foreach (KeyValuePair 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 item in keyValueDic)
{
row.Set(item.Key, item.Value);
}
table.Rows.Add(row);
}
}
}
}
return table;
}
public static Dictionary Split(string json)
{
if (string.IsNullOrEmpty(json) || !json.StartsWith("{") || !json.EndsWith("}"))
{
return null;
}
json = json.TrimStart('{').TrimEnd('}');
List items = new List();
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 keyValueDic = null;
if (items.Count > 0)
{
keyValueDic = new Dictionary();
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
}
}