C#Json數據類型


引用所對應框架的類庫文件,下載地址:http://json.codeplex.com/

在一般處理程序axhx中:

引用的命名空間:

using System.IO;
using Newtonsoft.Json;

using (DB2DataContext con = new DB2DataContext())
        {
            List<people> pl = con.people.ToList();
            JsonSerializer serializer = new JsonSerializer();
            StringWriter sw = new StringWriter();
            serializer.Serialize(new JsonTextWriter(sw), pl);
            string result = sw.GetStringBuilder().ToString();
            //context.Response.Write("{\"msg\":\"\",\"data\":\"" + result + "\"}");
            context.Response.Write(result);
            context.Response.End();
        }

同時還要partial一個類:

在類外部:

[JsonObject(MemberSerialization.OptIn)]

每條屬性之前: 

[JsonProperty]

這樣才能使數據有內容,如:

[JsonObject(MemberSerialization.OptIn)]
public partial class people
{
    [JsonProperty]
    public string names
    {
        get
        {
            return this._name;
        }
    }

    [JsonProperty]
    public string birthdays
    {
        get
        {
            return this._birthday.Value.ToString("yyyy年MM月dd日");
        }
    }
    [JsonProperty]
    public string sexs
    {
        get
        {
            return this._sex.Value ? "" : "";
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM