---
title: "使用JSON"
author: "kay2005"
date: "2008-01-08T17:41:00Z"
tags: ["IBM", "JavaScript", "Servlet", "Web", "json"]
slug: "json"
draft: false
lastmod: "2008-01-08T17:41:00Z"
id: 166415
url: "https://kay2008.iteye.com/blog/166415"
---

关于JSON的介绍可以到[http://www.json.org/json-zh.html](http://www.json.org/json-zh.html)看，上面写的很详细。  
在JavaScript中使用：

&lt;script&gt;  
    function test()  
    {  
        //json 对象  
        var peoper = {  
        p:[  
        {"name":"kay","sex":"man","age":"22"},  
        {"name":"Rose","sex":"girl","age":"21"}  
        ]};  
        alert(peoper.p\[0].name); //value  kay  
        alert(peoper.p\[1].sex); //value girl  
        //修改值  
        peoper.p\[0].age = "55";  
        peoper.p\[0].name = "FanKai";  
          
        alert(peoper.p\[0].name); //value  FanKai  
        alert(peoper.p\[0].age); //value 55

        //var ss = peoper.toJSONString();  
        alert(peoper);

    }  
&lt;/script&gt;

在Action（Servlet）中使用JSON：

    String js = request.getParameter("json");  
        try  
        {  
            JSONObject json = new JSONObject(js);  
            System.out.println("姓名："+json.getString("name"));  
            System.out.println("性别："+json.getString("sex"));  
        }  
        catch (JSONException e)  
        {  
            e.printStackTrace();  
        }

给客户端发送JSON：

JSONObject json = new JSONObject();  
json.append("name", "kay");  
json.append("sex", "boy");  
response.getWriter().print(json.toString());    

资料：  
http://www.ibm.com/developerworks/cn/web/wa-ajaxintro10/  
http://www.ibm.com/developerworks/cn/web/wa-ajaxintro11.html  
http://www.iteye.com/topic/71343  
http://www.json.org/json-zh.html  
http://www.iteye.com/topic/139477