public class User {  

    private String username;
private String password; public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }

  

普通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);

JAVA array,map 转 json 字符串的更多相关文章

  1. java普通对象和json字符串的互转

    一.java普通对象和json字符串的互转 java对象---->json 首先创建一个java对象: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...

  2. Java实现微信菜单json字符串拼接

    Java实现微信菜单json字符串拼接 微信菜单拼接json字符串方法 >>>>>>>>>>>>>>>> ...

  3. <摘录>Gson对Java嵌套对象和JSON字符串之间的转换

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,具有良好的跨平台特性.近几年来已经和XML一样成为C/S架构中广泛采用的数据格式.有关JSON的更多知识, ...

  4. Java对象转换成Json字符串是无法获得对应字段名

    问题: 代码中已经标注 @JSONField(name = "attrs") private String abc; public String getA() { return a ...

  5. 【Java_Spring】java解析多层嵌套json字符串

    java解析多层嵌套json字符串    

  6. Java 利用Gson将json字符串转换为List<Map<String, String>>

    json字符串类似于: [ { "userPhone": "123", "userNo": "123-2", " ...

  7. java封装对象转json字符串

    /** * Copyright (c) 2011-2015, James Zhan 詹波 (jfinal@126.com). * * Licensed under the Apache License ...

  8. java前端传入的json字符串保存到表中的方法

    表 service_goods_base 字段如下: 传入的json 字符串: servicePictureArray  :  [{"picServiceUrl": "h ...

  9. java解析多层嵌套json字符串

    java分别解析下面两个json字符串 package jansonDemo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjso ...

随机推荐

  1. 设计模式(Python)-策略模式

    本系列文章是希望将软件项目中最常见的设计模式用通俗易懂的语言来讲解清楚,并通过Python来实现,每个设计模式都是围绕如下三个问题: 为什么?即为什么要使用这个设计模式,在使用这个模式之前存在什么样的 ...

  2. 【小白的java成长系列】——windows下搭建和配置java环境

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/enson16855/article/details/25967851 基于非常多原因,还是得说说ja ...

  3. 使用位图文本工具BMFont从图片生成自定义字体

    bmfont工具如何使用 http://www.360doc.com/content/13/1206/12/14253074_334930801.shtml fnt各属性含义 http://www.2 ...

  4. hdfs调优

    本文章来自 hackershell.cn,转载请标注出处 描述 这篇文章主要从一些配置设置相关方面去调优Hadoop集群的笔记,内容来自网上或一些实践经验 1.HDFS审计日志 HDFS审计日志是一个 ...

  5. How to Configure Eclipse for Python --- 在eclipse中如何配置pydev

    From: http://www.rose-hulman.edu/class/csse/resources/Eclipse/eclipse-python-configuration.htm Pytho ...

  6. TS流解析 二 *****

    1.TS格式介绍 TS:全称为MPEG2-TS.TS即"Transport Stream"的缩写.它是分包发送的,每一个包长为188字节(还有192和204个字节的包).包的结构为 ...

  7. 1021 docker搭建mysql、网络模式、grid

    1.搭建并连接mysql服务 1.1.mysql官方命令 https://hub.docker.com/_/mysql/ #下载mysql镜像: docker pull mysql #启动mysql: ...

  8. Conv

    folly/Conv.h folly/Conv.h is a one-stop-shop for converting values across types. Its main features a ...

  9. Ubuntu 中安装 Docker

    检查 Device Mapper 是否存在 sch01ar@ubuntu:~$ ls -l /sys/class/misc/device-mapper 安装 Ubuntu 维护的版本 sch01ar@ ...

  10. python之数据驱动ddt

    下载ddt并安装 Pip install ddt 或者官网下载安装 http://ddt.readthedocs.io/en/latest/ https://github.com/txels/ddt ...