json札记
js 中读取JSON的方法探讨
方法一:函数构造定义法返回
var strJSON = "{name:'json name'}";//得到的JSON
var obj = new Function("return" + strJSON)();//转换后的JSON对象
alert(obj.name);//json name
方法二:js中著名的eval函数
var strJSON = "{name:'json name'}";//得到的JSON
var obj = eval( "(" + strJSON + ")" );//转换后的JSON对象
alert(obj.name);//json name
Ps:第二种方法需要注意的是,对象表达式{'name':'json name'}必须用“()”扩住,否则
var strJSON = "{name:'json name'}";
var obj = eval(strJSON);
alert(obj.constructor);//String 构造函数
alert(obj.name);//undefine
必须把对象表达式扩起来eval执行才能生成一个匿名对象!
C#操作json的类;
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace JsonMoster
{
public class JsonConvert
{
private List<Item> _items = new List<Item>();
public List<Item> Items
{
get { return _items; }
set { _items = value; }
}
public JsonConvert(params Item[] items)
{
_items.AddRange(items);
}
#region
// <summary>
// 将Json对象转换成Json对象串
//</summary>
//<returns>返回Json字符串jsonString</returns>
#endregion
public string ToJsonString()
{
List<string> jsonString = new List<string>();
foreach (Item itm in _items)
{
jsonString.Add(itm.ToString());
}
return "[" + string.Join(",", jsonString.ToArray()) + "]";
}
//json与对象的相互转换
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;
}
}
#region
/// <summary>
/// /// Json 数据对象
/// </summary>
///
#endregion
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;
}
}
}
}