用JavaScriptSerializer解析Json成.net Object

 

代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Jquery1.aspx.cs" Inherits="各种web练习.Jquery.Jquery1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script src="../Lib/jquery-1.3.2-vsdoc.js" type="text/javascript"></script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title></title>   
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
    
</div>
    
</form>
</body>
</html>

 

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace 各种web练习.Jquery
{

    
public class product
    {
        
int id;

        
public int Id
        {
            
get { return id; }
            
set { id = value; }
        }
        
string name;

        
public string Name
        {
            
get { return name; }
            
set { name = value; }
        }
        
decimal value;

        
public decimal Value
        {
            
get { return this.value; }
            
set { this.value = value; }
        }

     }


    
public class JackInfo
    {
        
string name;

        
public string Name
        {
            
get { return name; }
            
set { name = value; }
        }
        
int age;

        
public int Age
        {
            
get { return age; }
            
set { age = value; }
        }

        List
<family> family;

        
public List<family> Family
        {
            
get { return family; }
            
set { family = value; }
        }


    }


    
public class family
    {
        
string father;

        
public string Father
        {
            
get { return father; }
            
set { father = value; }
        }
        
string mother;

        
public string Mother
        {
            
get { return mother; }
            
set { mother = value; }
        }
        
string son;

        
public string Son
        {
            
get { return son; }
            
set { son = value; }
        }

    }


    
public partial class Jquery1 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
string ppp = "[{id:'1',name:'a',value:'5'},{id:'2',name:'b',value:'8'}]";
            
string jsonObject = "{ name: 'Jack', age: '30', job: 'singer', family:[ { father: 'Tom', mother: 

'Mary', son: 'Joe'},{ father: 'Tom2', mother: 'Mary2', son: 'Joe2'}]}";
           
            System.Web.Script.Serialization.JavaScriptSerializer a 
= new 

System.Web.Script.Serialization.JavaScriptSerializer();
            
            product[] pa;
            pa 
= a.Deserialize<product[]>(ppp);
            TextBox1.Text 
= pa[1].Name.ToString();

            JackInfo jack 
= a.Deserialize<JackInfo>(jsonObject);
            TextBox1.Text 
+= "\n"+jack.Family[1].Father;



           

        }
    }
}

 

 

posted @ 2009-12-31 11:27  KissFly  Views(4511)  Comments(0)    收藏  举报