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。


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