Json Class

Json Class
// ==============================================================================
// Created by Bndy at 12/06/2009
// Copyright (c) 2009 Bndy, All rights reserved.
// Welcome to my site http://www.bndy.net
//
// * * * * * * * * * * * * * * * *
// * Q Q : 8 1 7 9 5 7 0 5 *
// * M S N : bndy533@msn.com *
// * Email : bndy533@163.com *
// * * * * * * * * * * * * * * * *
//
// ------------------------------------------------------------------------------
// Json 类及相关操作方法
// 引用 System.Runtime.Serialization, System.ServiceModel, System.ServiceModel.Web。
// ==============================================================================

using System.Collections.Generic;

namespace Net.Bndy.Data
{
public class TinyJson
{
private List<Item> _items = new List<Item>();
public List<Item> Items
{
get { return _items; }
set { _items = value; }
}

public TinyJson(params Item[] items)
{
_items.AddRange(items);
}
/// <summary>
/// 将Json对象转换成Json对象串
/// </summary>
/// <returns>返回Json字符串</returns>
public string ToJsonString()
{
List
<string> lst = new List<string>();
foreach (Item itm in _items)
{
lst.Add(itm.ToString());
}
return "[" + string.Join(",", lst.ToArray()) + "]";
}
#region 静态方法开始
public static T JsonToObject<T>(string jsonString)
{
T result;
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer
= new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonString)))
{
result
= (T)serializer.ReadObject(stream);
}
return result;
}
public static string ObjectToJson(object o)
{
string result = string.Empty;
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer
= new System.Runtime.Serialization.Json.DataContractJsonSerializer(o.GetType());
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
serializer.WriteObject(stream, o);
byte[] dataBytes = new byte[stream.Length];
stream.Position
= 0;
stream.Read(dataBytes,
0, dataBytes.Length);
result
= System.Text.Encoding.UTF8.GetString(dataBytes);
}
return result;
}
# endregion 静态方法结束
/// <summary>
/// Json 数据对象
/// </summary>
public class Item
{
private List<Attribute> _attributes = new List<Attribute>();
public List<Attribute> Attributes
{
get { return _attributes; }
set { _attributes = value; }
}
public Item(params Attribute[] attributes)
{
_attributes.AddRange(attributes);
}
public override string ToString()
{
List
<string> lst = new List<string>();
foreach (Attribute attr in _attributes)
{
lst.Add(
"\"" + attr.Name + "\":\"" + attr.Value + "\"");
}
return "{" + string.Join(",", lst.ToArray()) + "}";
}
/// <summary>
/// 对象的属性类
/// </summary>
public class Attribute
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _value;
public string Value
{
get { return _value; }
set { _value = value; }
}
public Attribute(string attrName, string attrValue)
{
_name
= attrName;
_value
= attrValue;
}
}
}
}
}

 

posted @ 2010-03-22 23:12  bndy  阅读(557)  评论(0编辑  收藏  举报