Penson class
Using system;using system.collections.generic;using system.linq;using system.web;namespace Json_Demo{ public class Penson { private string _name; private int _age; private string _ address; public string address { get { return _address; } set { _address = value; } } public int Age { get { return _age; } set { if (_age < 100 && _ age > 0) { _age = value; } else { _age = 0; } } } public string Name { get { return _name; } set { _name = value; } } }}
Background code:
Using system;using system.collections.generic;using system.linq;using system.web;using system.web.ui;using system.web.ui.webcontrols;//introduction of related namespaces using system.web.script;using system.web.script.serialization;namespace json_demo{ public partial class webform1 : system.web.ui.page { protected void page_load (object sender, eventargs e) { Penson p1 = new penson (); p1. name = "JSON"; p1. Age = 120; p1. address = "Jiangxi Gao an"; javascriptserializer json1 = new javascriptserializer (); string str = json1. Serialize (p1);//json serialization session["STR1"] = str;//session page value } }} foreground <%@ page language= "C #" autoeventwireup= "true" codebehind= "WebForm1.aspx.cs" inherits= "Json_demo.webform1" %>//import namespace <%@ import namespace= "Json_demo" %><%@ import namespace= "System.Web.Script.Serialization" %><! doctype html>: 650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/4D/B3/wKioL1RXhF_BcvoIAADfqom1PAw324.jpg "title=" Qq20141103213108.jpg "alt=" Wkiol1rxhf_bcvoiaadfqom1paw324.jpg "/>
This article is from the "Day to Up" blog, so be sure to keep this source http://1433189426.blog.51cto.com/8055494/1571439
Serialization and deserialization of JSON in. Net