JSON格式的转换

JSON对象是JavaScipt对象的自定义对象

1.字符串转化为JSON对象,可用“(”,“)” 拼接

也可以用$.parseJSON()进行转化

2.JSON.stringify函数的作用是将一个javascript对象,转换为JSON格式的字符串。

3.JSONObject json=JSONObject.fromObject(data);     转化为JSON对象

4.Hero hero = (Hero)JSONObject.toBean(json,Hero.class); z转化为Hero对象

5.JSONObject json= new JSONObject();   创建Json对象

获取多数组的时候

String result =JSONSerializer.toJSON(heros).toString();

response.setContentType("text/html;charset=utf-8"); 
response.getWriter().print(result);
 
单个数据
          
        Hero hero = new Hero();
        hero.setName("盖伦");
        hero.setHp(353);
         
        JSONObject json= new JSONObject();
   
        json.put("hero", JSONObject.fromObject(hero));
        response.setContentType("text/html;charset=utf-8"); 
        response.getWriter().print(json);
原文地址:https://www.cnblogs.com/zxj-262410/p/8832024.html