wj0816

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

直接上代码:

JSONObject的的使用需要导入json-lib-2.4-jdk15.jar包,下载地址:http://mvnrepository.com/artifact/net.sf.json-lib/json-lib/2.4;

 

 

 1 package test;
 2 
 3 import net.sf.json.JSONObject;
 4 import testspringmysql.User;
 5 
 6 public class TestClass
 7 {
 8 
 9     public static void main(String args[])
10     {
11         //创建user对象
12         User user = new User();
13         user.setAge(10);
14         user.setName("ceshi");
15         
16         //转化成json对象
17         JSONObject jsonResult = JSONObject.fromObject(user);
18         //输出
19         System.out.println(jsonResult);
20 
21     }
22 }

user类

package testspringmysql;


public class User {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

结果:

 

posted on 2017-06-19 14:37  wj0816  阅读(12650)  评论(0编辑  收藏  举报