請看Button2_Click
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace jason_text3
{
public partial class WebForm1 : System.Web.UI.Page
{
public class prs
{
public List<person> pr = new List<person>();
}
public string output;
protected void Page_Load(object sender, EventArgs e)
{
person person1 = new person();
person1.Age = 18;
person1.Name = "tony";
person person2 = new person();
person1.Age = 19;
person1.Name = "jack";
prs prs = new prs();
prs.pr.Add(person1);
prs.pr.Add(person2);
output = Newtonsoft.Json.JsonConvert.SerializeObject(prs.pr);
Literal1.Text = output;
}
protected void Button1_Click(object sender, EventArgs e)
{
prs convert_pr=new prs();
convert_pr= Newtonsoft.Json.JsonConvert.DeserializeObject<prs>(output);
foreach (var item in convert_pr.pr)
{
Literal2.Text += item.Name;
}
}
public class person
{
//public int Age { get; set; }
public int Age { get; set; }
public string Name { get; set; }
}
protected void Button2_Click(object sender, EventArgs e)
{
prs getpersons = new prs();
getpersons.pr = getPersonInfo();
foreach (var p in getpersons.pr)
{
Literal2.Text += "Name = " + p.Name + "\t Age = " + p.Age + "\n";
}
}
public List<person>
getPersonInfo()
{
string jsonText = "[{'Age':20,'Name':'jack'},{'Age':22,'Name':'carry'}]";
List<person> personlist =Newtonsoft.Json.JsonConvert.DeserializeObject<List<person>>(jsonText);
return personlist;
}
}
}
|
沒有留言:
張貼留言