• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
RiXun
博客园    首页    新随笔    联系   管理    订阅  订阅

使用JSON

软件152 苏锐

在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());    

 

posted @ 2017-07-02 20:34  RiXun  阅读(238)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3