一个分享个人学习、开发经验的Blog,http://www.joyphper.net

JAVA josn-lib包,带示例文件

posted @ 2011-11-20 20:40 | 阅读:4395 | 评论:2 | 分类: Java

 

json-lib完整包,包含文件如下:

commons-beanutils.jar,

commons-collections.jar,

commons-lang-2.4.jar,

commons-logging.jar,

ezmorph-1.0.4.jar,

json-lib-2.3-jdk15.jar,

 

以及一个 示例类。

示例代码如下:

 

package test; 
 
import java.util.HashMap; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Map; 
 
import net.sf.json.JSONArray; 
import net.sf.json.JSONObject; 
 
public class TestJson { 
 
    //转换一个对象 
    public void objToJSON(){ 
        Student student = new Student(); 
        student.setAge(18); 
        student.setName("zhangsan"); 
        student.setSex("male"); 
        JSONObject jsonObject = JSONObject.fromObject(student); 
        System.out.println(jsonObject.toString()); 
    } 
    //{name:xxx,age:xxx,sex:xxx} 
    //转换一个链表 
    public void listOrSetToJSON(){ 
        List<Student> stuList = new LinkedList<Student>(); 
        for (int i = 0; i < 5; i++) { 
            Student stu = new Student(); 
            stu.setAge(i * 10 + 8); 
            stu.setName("zhang" + i); 
            stu.setSex("male"); 
            stuList.add(stu); 
        } 
        JSONArray jsonArrayFromList = JSONArray.fromObject(stuList); 
        System.out.println(jsonArrayFromList.toString()); 
        //{list:[{name:xxx,age:xxx,sex:xxx},{name:xxx,age:xxx,sex:xxx},{name:xxx,age:xxx,sex:xxx}]} 
    } 
    public void mapToJSON(){ 
        Map<String, Object> map = new HashMap<String, Object>(); 
        Teacher teacher = new Teacher(); 
        teacher.setAge(30); 
        teacher.setName("laoshi"); 
        teacher.setSex("male"); 
        map.put("teacher", teacher); 
        JSONObject jsonObjectFromMap = JSONObject.fromObject(map); 
        System.out.println(jsonObjectFromMap); 
    } 
    public static void main(String[] args) { 
        //new TestJson().objToJSON(); 
        //new TestJson().listOrSetToJSON(); 
        new TestJson().mapToJSON(); 
    } 
} 

 

Josn-lib完整文件包:2011112020500595.rar

TAG: java , json , lib

共有2条评论 发表评论>>

多多多 发表于:2011-11-23 15:46
正在做网站http://www.sznkyy.cn/。很有帮助,谢谢。
Gerry 发表于:2016-05-18 19:33
I do not even know how I ended up here, but I thought this post was great. I do not know who you are but definitely you are going to a famous blogger if you ar78n#&21e;t already Cheers!
点击换一张验证码