C#中使用JSON

摘自:http://blog.sina.com.cn/s/blog_59cb8a930100vlbr.html

 二、定義序列化的類

假如我們要轉化的JSON字符串格式爲:

{
    
"encoding":"UTF-8",
    
"plug-ins":["python","c++","ruby"],
    
"indent":{
        
"length":3,
        
"use_space":true
    }
}

然後編寫相應的序列化的類,注意下面類加的Attribute:

C# <wbr>中使用JSON[DataContract(Namespace = "http://coderzh.cnblogs.com")]
class Config
{
    [DataMember(Order 
= 0)]
    
public string encoding { getset; }
    [DataMember(Order 
= 1)]
    
public string[] plugins { getset; }
    [DataMember(Order 
= 2)]
    
public Indent indent { getset; }
}

[DataContract(Namespace 
= "http://coderzh.cnblogs.com")]
class Indent
{
    [DataMember(Order 
= 0)]
    
public int length { getset; }
    [DataMember(Order 
= 1)]
    
public bool use_space { getset; }
}

三、對象轉化爲JSON字符串

使用WriteObject方法:

C# <wbr>中使用JSON
var config = new Config(){
                         encoding 
= "UTF-8",
                         plugins 
= new string[]{"python""C++""C#"},
                         indent 
= new Indent(){ length = 4, use_space = false}
                         };


摘自:http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html


在.NET中如何使用JSON

說到在.net中使用JSON,就不得不提到JSON.NET,它是一個非常著名的在.net中處理JSON的工具,我們最常用的是下面兩個功能。

1,通過序列化將.net對象轉換爲JSON字符串

在web開發過程中,我們經常需要將從數據庫中查詢到的數據(一般爲一個集合,列表或數組等)轉換爲JSON格式字符串傳回客戶端,這就需要進行序列化,這裏用到的是JsonConvert對象的SerializeObject方法。其語法格式爲:JsonConvert.SerializeObject(object),代碼中的”object”就是要序列化的.net對象,序列化後返回的是json字符串。

比如,現在我們有一個TStudent的學生表,表中的字段和已有數據如圖所示

json05json06

從表中我們可以看到一共有五條數據,現在我們要從數據庫中取出這些數據,然後利用JSON.NET的JsonConvert對象序列化它們爲json字符串,並顯示在頁面上。C#代碼如下

複製代碼
protected void Page_Load(object sender, EventArgs e)
        {
            using (L2SDBDataContext db = new L2SDBDataContext())
            {
                List<Student> studentList = new List<Student>();
                var query = from s in db.TStudents
                            select new { 
                                StudentID=s.StudentID,
                                Name=s.Name,
                                Hometown=s.Hometown,
                                Gender=s.Gender,
                                Brithday=s.Birthday,
                                ClassID=s.ClassID,
                                Weight=s.Weight,
                                Height=s.Height,
                                Desc=s.Desc
                            };
                foreach (var item in query)
                {
                    Student student = new Student { StudentID=item.StudentID,Name=item.Name,Hometown=item.Hometown,Gender=item.Gender,Brithday=item.Brithday,ClassID=item.ClassID,Weight=item.Weight,Height=item.Height,Desc=item.Desc};
                    studentList.Add(student);
                }
                lbMsg.InnerText = JsonConvert.SerializeObject(studentList);
            }
        }
複製代碼

輸出結果

json07

從圖中我們可以看到,數據庫中的5條記錄全部取出來並轉化爲json字符串了。




PS:

return Json(JsonConvert.SerializeObject(studentList)); 返回到前臺html頁的格式爲string,可以用eval()轉化爲object。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章