C((zhi)jsonのプログレッシブとアンチプログレッシブの実例コードを実現する。
二つの方法には一例があります。一つは汎型集合で記憶する対象で、その後、集合を逐次列化します。第二は、直接的な順序付けのオブジェクト
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Configuration;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.IO;
using System.Text;
namespace WebApplication1
{
// : System.Web.Script.Serialization JavaScriptSerializer
[Serializable]
public class Person
{
private int id;
/// <summary>
/// id
/// </summary>
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
/// <summary>
///
/// </summary>
public string Name
{
get { return name; }
set { name = value; }
}
}
// : System.Runtime.Serialization.Json DataContractJsonSerializer
// IgnoreDataMember: ,DataMember: , DataMember DataContract DataMember 。
[DataContract]
public class Person1
{
[IgnoreDataMember]
public int Id { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "sex")]
public string Sex { get; set; }
}
public partial class _Default : System.Web.UI.Page
{
string constr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
Person p1 = new Person();
p1.Id = 1;
p1.Name = "dxw";
Person p2 = new Person();
p2.Id = 2;
p2.Name = "wn";
List<Person> listperson = new List<Person>();
listperson.Add(p1);
listperson.Add(p2);
JavaScriptSerializer js = new JavaScriptSerializer();
//json
string s = js.Serialize(listperson);
Response.Write(s);
//
Person1 p11 = new Person1();
p11.Id = 1;
p11.Name = "hello";
p11.Sex = " ";
DataContractJsonSerializer json = new DataContractJsonSerializer(p11.GetType());
string szJson = "";
//
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, p11);
szJson = Encoding.UTF8.GetString(stream.ToArray());
Response.Write(szJson);
}
//
//using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
//{
// DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People));
// Person1 _people = (Person1)serializer.ReadObject(ms);
//}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(constr);
}
}
である。