C#将json字符串解析成对象
jdata: jdata
}, function(data, textStatus) {
using System.Collections.Generic;
{
public int id { set; get; }
public String email { set; get; }
public int age { set; get; }
}
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Deserialize<T>(jsonText);
}
{
string j = DataReust[i].email;
}
为了在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();