码迷,mamicode.com
首页 > Web开发 > 详细

JSONObject和JSONArray的简单使用

时间:2016-09-08 18:34:03      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

一. jar包

  1. commons-lang.jar
  2. commons-beanutils.jar
  3. commons-collections.jar
  4. commons-logging.jar 
  5. ezmorph.jar
  6. json-lib-2.2.2-jdk15.jar

二. 代码

package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Test {

    public static void main(String[] args) {
        new Test().test1();
        new Test().test2();
        new Test().test3();
        new Test().test4();
        new Test().test5();
        new Test().test6();
        new Test().test7();
    }

    /**
     * JSON字符串转JSONObject对象
     */
    public void test1() {
        String jsonStr = "{\"name\":\"ZhangSan\",\"sex\":\"boy\",\"age\":18}";
        JSONObject jsonObj = JSONObject.fromObject(jsonStr);
        System.out.println(jsonObj.toString());
    }

    /**
     * 简单JSONObject对象转java对象
     */
    public void test2() {
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("name", "LiSi");
        jsonObj.put("sex", "girl");
        jsonObj.put("age", 17);
        Student student = (Student) JSONObject.toBean(jsonObj, Student.class);
        System.out.println(student.getName() + " | " + student.getSex() + " | " + student.getAge());
    }

    /**
     * 复杂JSONObject对象转java对象
     */
    @SuppressWarnings("rawtypes")
    public void test3() {
        List<Student> students = new ArrayList<Student>();
        students.add(new Student("ZhangSan", "boy", 18));
        students.add(new Student("LiSi", "girl", 17));

        BanJi banji = new BanJi();
        banji.setBanJiName("日语二班");
        banji.setStudents(students);

        JSONObject jsonObj = JSONObject.fromObject(banji);
        Map<String, Class> classMap = new HashMap<String, Class>();
        classMap.put("students", Student.class);
        BanJi banji2 = (BanJi) JSONObject.toBean(jsonObj, BanJi.class, classMap);
        System.out.println(banji2.getStudents().get(0).getName()); // 验证转换是否成功
    }

    /**
     * 简单java集合对象转JSONArray
     */
    public void test4() {
        List<Student> students = new ArrayList<Student>();
        students.add(new Student("ZhangSan", "boy", 18));
        students.add(new Student("LiSi", "girl", 17));
        JSONArray jsonArray = JSONArray.fromObject(students);
        System.out.println(jsonArray.toString());
    }

    /**
     * JSONArray转java集合对象
     */
    @SuppressWarnings({ "unchecked", "deprecation" })
    public void test5() {
        JSONObject jsonObj1 = new JSONObject();
        jsonObj1.put("name", "ZhangSan");
        jsonObj1.put("sex", "boy");
        jsonObj1.put("age", 18);

        JSONObject jsonObj2 = new JSONObject();
        jsonObj2.put("name", "lisi");
        jsonObj2.put("sex", "girl");
        jsonObj2.put("age", 17);

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(0, jsonObj1);
        jsonArray.add(1, jsonObj2);

        List<Student> students3 = JSONArray.toList(jsonArray, Student.class);
        System.out.println(students3.get(0).getName());
        System.out.println(students3.get(1).getName());
    }

    /**
     * 复杂java集合对象转JSONArray
     */
    @SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
    public void test6() {
        BanJi banji1 = new BanJi();
        banji1.setBanJiName("日语一班");
        List<Student> students1 = new ArrayList<Student>();
        students1.add(new Student("ZhangSan", "boy", 18));
        banji1.setStudents(students1);

        BanJi banji2 = new BanJi();
        banji2.setBanJiName("日语二班");
        List<Student> students2 = new ArrayList<Student>();
        students1.add(new Student("LiSi", "girl", 17));
        banji2.setStudents(students2);

        List<BanJi> banjiList = new ArrayList<BanJi>();
        banjiList.add(banji1);
        banjiList.add(banji2);

        JSONArray jsonArray = JSONArray.fromObject(banjiList);
        Map<String, Class> classMap = new HashMap<String, Class>();
        classMap.put("students", Student.class);
        List<BanJi> banjiList2 = JSONArray.toList(jsonArray, BanJi.class, classMap);
        System.out.println(banjiList2.get(0).getStudents().get(0).getName());

    }

    /**
     * 拆分JSONArray为JSONObject
     */
    public void test7() {
        JSONObject jsonObj1 = new JSONObject();
        jsonObj1.put("name", "ZhangSan");
        jsonObj1.put("sex", "boy");
        jsonObj1.put("age", 18);

        JSONObject jsonObj2 = new JSONObject();
        jsonObj2.put("name", "lisi");
        jsonObj2.put("sex", "girl");
        jsonObj2.put("age", 17);

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(0, jsonObj1);
        jsonArray.add(1, jsonObj2);

        JSONObject object = (JSONObject) jsonArray.get(0);
        System.out.println(object.toString());
    }

}

 

package com.test;

import java.util.List;

/**
 * 班级类
 */
public class BanJi {

    private String banJiName; // 班级名

    private List<Student> students; // 学生

    /**
     * 构造函数
     */
    public BanJi() {
        super();
    }

    // getters/setters(略)
}
package com.test;

/**
 * 学生类
 */
public class Student {

    private String name; // 姓名

    private String sex; // 性别

    private int age; // 年龄

    /**
     * 构造函数
     */
    public Student() {
        super();
    }

    /**
     * 构造函数
     */
    public Student(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    // getters/setters(略)
}

 

JSONObject和JSONArray的简单使用

标签:

原文地址:http://www.cnblogs.com/zj0208/p/5853733.html

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