zoukankan      html  css  js  c++  java
  • JAVA array,map 转 json 字符串

    1. public class User {  
    2.       
    3.     private String username;  
    4.     private String password;  
    5.       
    6.     public String getUsername() {  
    7.         return username;  
    8.     }  
    9.     public void setUsername(String username) {  
    10.         this.username = username;  
    11.     }  
    12.     public String getPassword() {  
    13.         return password;  
    14.     }  
    15.     public void setPassword(String password) {  
    16.         this.password = password;  
    17.     }  
    18.       
    19. }  


    普通JavaBean(以User为例)转成json格式 
    1.转成JSONArray类型 
    User user = new User(); 
    user.setUsername("cxl"); 
    user.setPassword("1234"); 
    JSONArray json = JSONArray.fromObject(user); 
    System.out.println(json);//[{"password":"1234","username":"cxl"}] 
    response.getWriter().print(json.toString()); 
    在js文件取数据 
    $.getJSON("http://localhost:8080/jQueryDemo/servlet/UserServlet",null,function(data) { 
          alert(data[0].username); 
          alert(data[0].password); 
    }); 
    2.转成JSONObject类型 
    JSONObject jsonObj = JSONObject.fromObject(user); 
    System.out.println(jsonObj);//{"password":"1234","username":"cxl"} 
    response.getWriter().print(jsonObj); 
    在js文件取数据 
    alert(data.username); 
    alert(data.password); 

    List转成json格式 
    List<User> users = new ArrayList<User>(); 
    User user = new User(); 
    user.setUsername("cxl"); 
    user.setPassword("1234"); 
    User u = new User(); 
    u.setUsername("lhl"); 
    u.setPassword("1234"); 
    users.add(user); 
    users.add(u); 
    1.转成JSONArray类型 
    JSONArray json = JSONArray.fromObject(users); 
    System.out.println(json.toString()); 
    //[{"password":"1234","username":"cxl"},{"password":"1234","username":"lhl"}] 
    response.getWriter().print(json.toString()); 
    js中取数据:alert(alert(data[0].username)); 
    2.转成JSONObject类型 
    不可直接使用JSONObject jsonObj = JSONObject.fromObject(users); 
    可通过下列方式 
    JSONObject jsonObj = new JSONObject(); 
    jsonObj.put("users", users); 
    jsonObj.put("u", u); 
    System.out.println(jsonObj); 
    {"users":[{"password":"1234","username":"cxl"},{"password":"1234","username":"lhl"}], 
      "u":{"password":"1234","username":"lhl"}} 
    response.getWriter().print(jsonObj); 
    js中取数据:alert(data.users[0].username); 
               alert(data.u[0].username); 

    Map转成json格式 
    Map<String,Object> map = new HashMap<String,Object>(); 
    map.put("users", users); 
    map.put("u", u); 
    1.转成JSONArray类型 
    JSONArray json = JSONArray.fromObject(map); 
    System.out.println(json.toString());// 
    [{"users":[{"password":"1234","username":"cxl"},{"password":"1234","username":"lhl"}],"u":{"password":"1234","username":"lhl"}}] 
    response.getWriter().print(json.toString); 
    js中取数据:alert(data[0].users[0].username); 
    2.转成JSONObject类型 
    JSONObject json = JSONObject.fromObject(map); 
    System.out.println(json);// 
    {"user":[{"password":"1234","username":"cxl"},{"password":"1234","username":"lhl"}],"u":{"password":"1234","username":"lhl"}} 
    response.getWriter().print(json); 
    js中取数据:alert(data.user[0].username); 

  • 相关阅读:
    关于求 p_i != i and p_i != i+1 的方案数的思考过程
    poj 3041 Asteroids 二分图最小覆盖点
    poj 1325 Machine Schedule 最小顶点覆盖
    poj 1011 Sticks 减枝搜索
    poj 1469 COURSES 最大匹配
    zoj 1516 Uncle Tom's Inherited Land 最大独立边集合(最大匹配)
    Path Cover (路径覆盖)
    hdu 3530 SubSequence TwoPoint单调队列维护最值
    zoj 1654 Place the Rebots 最大独立集转换成二分图最大独立边(最大匹配)
    poj 1466 Girls and Boys 二分图最大独立子集
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/6661525.html
Copyright © 2011-2022 走看看