日期:2014-05-18 浏览次数:20539 次
/// <summary> /// Serialize object to string of json format /// </summary> private string JsonSerializer<T>(T t) { var ser = new DataContractJsonSerializer(typeof(T)); var ms = new MemoryStream(); ser.WriteObject(ms, t); string jsonString = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return jsonString; } /// <summary> /// Deserialize string of json format to object /// </summary> private T JsonDeserialize<T>(string jsonString) { var ser = new DataContractJsonSerializer(typeof(T)); var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); var obj = (T)ser.ReadObject(ms); return obj; }
------解决方案--------------------
public static string ToJson(object obj)
{
System.Web.Script.Serialization.JavaScriptSerializer javaSer = new System.Web.Script.Serialization.JavaScriptSerializer();
return javaSer.Serialize(obj);
}
------解决方案--------------------
把数据保存在ilist中.遍历ilist.
stringbuilder str=new stringbuilder("[{");
str.append("")//你的操作.
...
...
...
str.append("}]");
接着str.ToString();
------解决方案--------------------
System.Web.Script.Serialization.JavaScriptSerializer
直接把你的实体类转化为json