C#将json字符串解析成对象

首先我们在客户端生成json字符串,通过ajax把该字符串传到服务器端
 
//这是一个以id,email,age的json字符串
  var jdata="[{\"id\":0,\"email\":\"abccd\",\"age\":0},{\"id\":1,\"email\":\"abc1\",\"age\":2}]";
 
//将这个json字符串,传递到PaperMark.ashx侧处理
 $.post("AJAX/PaperMark.ashx", {
                            jdata: jdata
                          
                        }, function(data, textStatus) {
    });
 
在PaperMark.ashx我们要引用下面的命名空间
using System.Web.Script.Serialization;
using System.Collections.Generic;
 
 string jsonData = context.Request.Form["jdata"];  //取得这个json字符串
 
//做一个类id,email,age和json格式一致
  public class Person
    {
        public int id { set; get; }
        public String email { set; get; }
        public int age { set; get; }
    }
 
//这个一个反序列化的方法,用于返回泛型集合
 public static T JSONToObject<T>(string jsonText)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        return jss.Deserialize<T>(jsonText);
    }
//调用上面这个方法,把json字符串传进去,就得到一个对象集合了
 List<Person> DataReust = JSONToObject<List<Person>>(jsonData);
        for (int i = 0; i < DataReust.Count; i++)
        {
            string j = DataReust[i].email;
        }
 
注意点:
json字符串的构造一定要正确。要不然解析不出来。

为了在js端更为方便的构造出正确的json对象,我做了一些函数用于生成json字符串

             var jstr = "";
              var jsData = "[";   //这个就是最后生成的json字符串
            
              function AddJsonItemStart() {
                  jstr = "";
              }

              //vtype==int的时候不加引号

              function AddJsonItem(name, val, vtype) {

                  if (jstr == "") {
                      //为整型的情况下面
                      if (vtype == "int") {
                          jstr = "{\"" + name + "\":" + val + ",";
                      }
                      else {
                          //为字符串的情况下面
                          jstr = "{\"" + name + "\":\"" + val + "\",";
                      }

                  }
                  else {
                      if (vtype == "int") {
                          jstr = jstr + "\"" + name + "\":" + val + ",";
                      }
                      else {
                          jstr = jstr + "\"" + name + "\":\"" + val + "\",";
                      }


                  }
              }

              function AddJsonItemOK() {
                  jstr = jstr.substring(0, jstr.length - 1) + "},";
                  jsData = jsData + jstr;
              }

              function AddJsonEnd() {
                  jsData = jsData.substring(0, jsData.length - 1) + "]";
              }

           使用方法:

                  AddJsonItemStart();
                  AddJsonItem("id", 1, "int");
                  AddJsonItem("email", "abc", "string");
                  AddJsonItem("age", 5, "int");
                  AddJsonItemOK();

                  AddJsonItemStart();
                  AddJsonItem("id", 2, "int");
                  AddJsonItem("email", "cef", "string");
                  AddJsonItem("age", 6, "int");
                  AddJsonItemOK();
                  AddJsonEnd();

分类: ASP.NET
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/p/3175252.html,如需转载请自行联系原作者
上一篇:linux里的驱动接口


下一篇:QString::arg()//用字符串变量参数依次替代字符串中最小数值