public partial class Json : System.Web.UI.Page
{
List<Person> PrList = new List<Person>();
String strjson = null;
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
Person pr = new Person();
pr.id = i;
pr.name = "张_" + i;
if (i % 2 == 0)
pr.sex = "男";
else
pr.sex = "女";
pr.age = (18 + i);
PrList.Add(pr);
}
strjson += "[";
foreach (Person var in PrList)
{
if (strjson.Contains("{"))
strjson += ",";
strjson += "{\"Id\":" + var.id + ",\"Name\":\""+var.name+"\",\"Sex\":\"" + var.sex + "\",\"Age\":" + var.age+"}";
}
strjson += "]";
Label1.Text = "解析Json";
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = strjson;
JArray jsondataObject = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(strjson);
for (int i = 0; i < jsondataObject.Count;i++ )
{
Response.Write(jsondataObject[i]["Id"].ToString() + "," + jsondataObject[i]["Name"].ToString() + "," + jsondataObject[i]["Sex"].ToString() + "," + jsondataObject[i]["Age"].ToString()+",<br/>");
}
}
}
public class Person
{
public int id;
public string name;
public string sex;
public int age;
}
使用此方法需要引用Newtonsoft.Json.dll 适用于.Net2.0及以上版本 如果找不到合适的可QQ联系我