asp.net的JSONHelper 类
调用方法:
JSONHelper.cs
JSONHelper json = new JSONHelper();
json.success = true;
json.AddItem("aid", "1");
json.ItemOk();
json.totlalCount=10;
string jsons = "";
if (json.totlalCount > 0)
{
jsons = json.ToString();
}
else
{
jsons = @"{success:false}";
}
return jsons;
json.success = true;
json.AddItem("aid", "1");
json.ItemOk();
json.totlalCount=10;
string jsons = "";
if (json.totlalCount > 0)
{
jsons = json.ToString();
}
else
{
jsons = @"{success:false}";
}
return jsons;
public class JSONHelper
{
//对应JSON的singleInfo成员
public string singleInfo = string.Empty;
protected string _error = string.Empty;
protected bool _success = true;
protected long _totalCount = 0;
protected System.Collections.ArrayList arrData = new ArrayList();
protected System.Collections.ArrayList arrDataItem = new ArrayList();
public JSONHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
//对应于JSON的success成员
public bool success
{
get
{
return _success;
}
set
{
//如设置为true则清空error
if (success) _error = string.Empty;
_success = value;
}
}
//对应于JSON的error成员
public string error
{
get
{
return _error;
}
set
{
//如设置error,则自动设置success为false
if (value != "") _success = false;
_error = value;
}
}
public long totlalCount
{
get { return _totalCount; }
set { _totalCount = value; }
}
//重置,每次新生成一个json对象时必须执行该方法
public void Reset()
{
_success = true;
_error = string.Empty;
singleInfo = string.Empty;
arrData.Clear();
arrDataItem.Clear();
}
public void AddItem(string name, string value)
{
arrData.Add("/"" + name + "/":" + "/"" + value + "/"");
}
public void ItemOk()
{
arrData.Add("<BR>");
//返回总记录条数
totlalCount++;
}
//序列化JSON对象,得到返回的JSON代码
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("{");
sb.Append("/"totalCount/":/"" + _totalCount.ToString() + "/",");
sb.Append("/"success/":" + _success.ToString().ToLower() + ",");
sb.Append("/"error/":/"" + _error.Replace("/"", "///"") + "/",");
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();
}
public string ToSingleString()
{
StringBuilder sb = new StringBuilder();
sb.Append("{");
sb.Append("success:" + _success.ToString().ToLower() + ",");
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();
}
}
{
//对应JSON的singleInfo成员
public string singleInfo = string.Empty;
protected string _error = string.Empty;
protected bool _success = true;
protected long _totalCount = 0;
protected System.Collections.ArrayList arrData = new ArrayList();
protected System.Collections.ArrayList arrDataItem = new ArrayList();
public JSONHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
//对应于JSON的success成员
public bool success
{
get
{
return _success;
}
set
{
//如设置为true则清空error
if (success) _error = string.Empty;
_success = value;
}
}
//对应于JSON的error成员
public string error
{
get
{
return _error;
}
set
{
//如设置error,则自动设置success为false
if (value != "") _success = false;
_error = value;
}
}
public long totlalCount
{
get { return _totalCount; }
set { _totalCount = value; }
}
//重置,每次新生成一个json对象时必须执行该方法
public void Reset()
{
_success = true;
_error = string.Empty;
singleInfo = string.Empty;
arrData.Clear();
arrDataItem.Clear();
}
public void AddItem(string name, string value)
{
arrData.Add("/"" + name + "/":" + "/"" + value + "/"");
}
public void ItemOk()
{
arrData.Add("<BR>");
//返回总记录条数
totlalCount++;
}
//序列化JSON对象,得到返回的JSON代码
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("{");
sb.Append("/"totalCount/":/"" + _totalCount.ToString() + "/",");
sb.Append("/"success/":" + _success.ToString().ToLower() + ",");
sb.Append("/"error/":/"" + _error.Replace("/"", "///"") + "/",");
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();
}
public string ToSingleString()
{
StringBuilder sb = new StringBuilder();
sb.Append("{");
sb.Append("success:" + _success.ToString().ToLower() + ",");
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();
}
}
url:http://greatverve.cnblogs.com/archive/2011/07/06/asp-net-jsonhelper.html
.NET通用JSON解析/构建类的实现(c#)
.NET通用JSON解析/构建类的实现(c#)
在.NET Framework 3.5中已经提供了一个JSON对象的序列化工具,但是他是强类型的,必须先按JSON对象的格式定义一个类型,并将类型加上JSON序列化特性。本文将试图提供一个高度灵活的JSON通用类型(JsonObject),实现对JSON的解析及序列化。
假设JSON对象内容如下:
使用JsonObject来构建,可选择以下三种方式:
方式一:
方式二:
方式三:
获取Json对象属性值的方法,也有三种方式:
直接使用ToString函数,将JsonObject转换为Json字符串:
注意:
我在重载ToString函数时,并没有将字符串转换为JavsScript字符串类型(即对需要转义的字符的处理),当然,要实现也是极其简单的。另外,对于带String参数的ToString,我也为做特殊处理,感兴趣的朋友可自行实现。
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。