Ajax基础:3.Json

1.Json将复杂对象序列化为一个字符串,在浏览器端再将字符串反序列化为Javascript可读取的对象

2.使用方法:JavaScriptSerializer().Serialize(p);

            JavaScriptSerializer jss = new JavaScriptSerializer();
            string json = jss.Serialize(new Person(){Name="Tom",Age=20});
            context.Response.Write(json);

3.页面中:

                var person = $.parseJSON(data);
                alert(person.Name);

4.数组

            JavaScriptSerializer jss = new JavaScriptSerializer();
            Person[] ps =new Person[]{ new Person() { Name = "Tom", Age = 20 }, new Person() { Name = "Jem", Age = 20 }};
            string json=jss.Serialize(ps);
            context.Response.Write(json);

 

                var person = $.parseJSON(data);
                alert(person[1].Name);

posted @ 2012-06-10 23:02  Xyang  阅读(149)  评论(0编辑  收藏  举报
hi