ASP.NET ashx文件如何客户端提交的JSON字符串转换成JSON对象然后把值读出来?

如何把一下JSON读出来(代码详细一点家悬赏)
string Json = { "people": [{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },{ "firstName":"Jason","lastName":"Hunter", "email": "bbbb"},{ "firstName": "Elliotte", "lastName":"Harold","email": "cccc" }]}

using System.Runtime.Serialization.Json;

public class infoObject
{
public String name { get; set; }
public String birthday { get; set; }
public String id { get; set; }
public String email { get; set; }

public infoObject()
{
name = "";
birthday = "";
id = "";
email = "";
}

public infoObject(String newname, String newbirthday, String newid, String newemail)
{
name = newname;
birthday = newbirthday;
id = newid;
email = newemail;
}

}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
infoObject info = new infoObject("peng","19920412","342423188209121234","[email protected]");
string szJson;
//将对象转成字符串
DataContractJsonSerializer json = new DataContractJsonSerializer(info.GetType());
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, info);//把对象填充到流里(关键)
szJson = Encoding.UTF8.GetString(stream.ToArray());//按指定格式把流编码成字符串
Response.Write( "对象转成字符串:<br>"+szJson+"<br>");
}

//将字符串转成对象
//infoObject obj = Activator.CreateInstance<infoObject>();
infoObject obj = new infoObject();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj=(infoObject)serializer.ReadObject(ms);//将含有字符串的流转成对象
}
Response.Write("字符串转对象:"+obj.name + "<br>" + obj.birthday + "<br>" + obj.id + "<br>" + obj.email);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-26
jsonSerialization,JavaScriptSerializer自已百度吧.
相似回答