ITeye博客

使用JSON

kay2005 · 2008-01-08 17:41 · [随笔分类]Java EE · json · IBM · JavaScript · Web · Servlet · Markdown

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

<script>
    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);

    }
</script>

在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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。