码迷,mamicode.com
首页 > 编程语言 > 详细

javaBean List Map json(转)

时间:2014-07-03 20:10:54      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   使用   

1、依赖的包

根据:http://json-lib.sourceforge.net/ 可见json-lib使用需要依赖一下包:

 bubuko.com,布布扣

下载地址:http://download.csdn.net/download/zfxinli/2117292  或百度盘:http://pan.baidu.com/s/1hq5kGQC

非常有用的参考:

(1)json在线转换器:http://json.parser.online.fr/

(2)json介绍:http://www.json.org/json-zh.html

2、普通JavaBean

(1)基本JavaBean

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;
    }
}

(2)转化成JSONArray

User user = new User();
user.setUsername("lp");
user.setPassword("1234");
JSONArray json = JSONArray.fromObject(user);
System.out.println(json);
//结果:[{"password":"1234","username":"lp"}]

(3)转化成JSONObject

User user = new User();
user.setUsername("lp");
user.setPassword("1234");
JSONObject json = JSONObject.fromObject(user);
System.out.println(json);
//结果;{"password":"1234","username":"lp"}

 

3、List<User>

(1)转化成JSONArray

List<User>users = new ArrayList<>();
User user = new User();
user.setUsername("lp");
user.setPassword("1234");
User userSecond = new User();
userSecond.setUsername("ch");
userSecond.setPassword("4567");
users.add(user);
users.add(userSecond);
JSONArray json = JSONArray.fromObject(users);
System.out.println(json);
//结果:[{"password":"1234","username":"lp"},{"password":"4567","username":"ch"}]

(2)转化成JSONObject

不可以直接通过JSONObject jsonObj = JSONObject.fromObject(users);进行

可以通过如下方式:

JSONObject json = new JSONObject();
json.put("users", users);
json.put("userSecond", userSecond);
System.out.println(json);
//结果:{"users":[{"password":"1234","username":"lp"},{"password":"4567","username":"ch"}],"userSecond":{"password":"4567","username":"ch"}}

  

4、Map

(1)转化成JSONArray

Map<String, Object>map = new HashMap<String, Object>();
map.put("users", users);
map.put("user", user);
JSONArray json = JSONArray.fromObject(map);
System.out.println(json);
//结果:[{"users":[{"password":"1234","username":"lp"},{"password":"4567","username":"ch"}],"user":{"password":"1234","username":"lp"}}]

(2)转化成JSONObject

Map<String, Object>map = new HashMap<String, Object>();
map.put("users", users);
map.put("user", user);
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
//结果:{"users":[{"password":"1234","username":"lp"},{"password":"4567","username":"ch"}],"user":{"password":"1234","username":"lp"}}
来源:http://blog.sina.com.cn/s/blog_a15aa56901017i2x.html

javaBean List Map json(转),布布扣,bubuko.com

javaBean List Map json(转)

标签:style   blog   http   java   color   使用   

原文地址:http://www.cnblogs.com/lpshou/p/3821207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!