JSON格式的字符串转换为java对象

package cn.itcast.caoke.json;java

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

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.junit.Test;数组

public class Json2Java {
    
    /*
     * 类中包含类的写法
     * caoke===24----cn.itcast.caoke.json.Student@3abc8e1e
     * caoke2===12
     * liyong===28----cn.itcast.caoke.json.Student@311671b2
     * liyong2===19
     */
    @Test
    public void json2beanTest(){
        //建立一个json字符串
        String strJson = "[{\"name\":\"caoke\",\"age\":\"24\",\"student\":{\"name\":\"caoke2\",\"age\":\"12\"}}," +
                "{\"name\":\"liyong\",\"age\":\"28\",\"student\":{\"name\":\"liyong2\",\"age\":\"19\"}}]" ;
        //json字符串转换为json数组
        // 使用JsonArray 是为了多条转换 一条能够直接使用JSONObject转换
        JSONArray jsonArray = JSONArray.fromObject(strJson);
         /*
         * 表示json类中包含的子类,关联的对象
         * 当有多个对象的时候,继续put,属性名,和Class类,
         * list对象 一样 放 属性名,和 list 的泛型
         * 在JSONObject.toBean的时候若是转换的类中有集合,能够先定义Map<String, Class> classMap = new HashMap<String,Class>();
         * 在classMap中put你要转换的类中的集合名,像:classMap.put("student", Student.class);
         * 而后在toBean()的时候把参数加上, 
         * 像:Student student=(Student)JSONObject.toBean(jsonObject, Person.class, classMap); 
         */
        Map<String, Class<Student>> map = new HashMap<String, Class<Student>>();
        map.put("student", Student.class);
        //新建对象数组,用来放json转换的bean对象
        Object[] objects = new Object[jsonArray.size()] ;
        for(int i = 0 ;i<objects.length;i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            // 遍历json数组,开始转换对象    第一个  要转换的json对象 第二个是要转换成的类对象  第三个 是类对象中的引用对象属性
            // 用map来存储  key是属性名 value 是引用类型
            objects[i] = JSONObject.toBean(jsonObject, Person.class, map);
        }
        // 遍历引用类型 
        for(int i = 0 ;i<objects.length;i++){
            Person person = (Person) objects[i];
            System.out.println(person.getName()+"==="+person.getAge()+"----"+person.getStudent());
            Student student = person.getStudent();
            System.out.println(student.getName()+"==="+student.getAge());
            
        }
    }
    
    /*
     * 单独对象,没有引用其余对象的实体类
     * 直接转换
     *  caoke---24
        liyong---19
     */
    @Test
    public void json2beanTest2(){
        // strJson 字符串
        String strJson = "[{\"name\":\"caoke\",\"age\":\"24\"},{\"name\":\"liyong\",\"age\":\"19\"}]" ;
        // 字符串转换为字符数组
        // 使用JsonArray 是为了多条转换 一条能够直接使用JSONObject转换
        JSONArray jsonArray = JSONArray.fromObject(strJson);
        Object [] objects = new Object[jsonArray.size()];
        for(int i =0;i<objects.length;i++){
            // 获得每个json对象,json对象转换为类
            JSONObject jsonObject = JSONObject.fromObject(jsonArray.get(i));
            objects[i] = JSONObject.toBean(jsonObject, Student.class);
        }
        for(int i =0;i<objects.length;i++){
            Student student = (Student)objects[i] ;
            System.out.println(student.getName()+"---"+student.getAge());
        }
        
    }
    
    /*
     * 把map类型的json字符串转换成一个map集合
     * 把json的键值对所有转换为map
     * 直接转换
     *  caoke
        {"name":"caoke","age":"24"}
        caoke
        24
     */
    @Test
    public void json2Map(){
        // strJson 字符串
        String strJson = "{\"m\":\"caoke\",\"map\":{\"name\":\"caoke\",\"age\":\"24\"}}" ;
        // 字符串转换为字符数组
        JSONObject jsonObject = JSONObject.fromObject(strJson);
        Map<Object, Object> map =(Map) jsonObject ;
        System.out.println(map.get("m"));
        System.out.println(map.get("map"));
        // 再次把值转化为map
        jsonObject = JSONObject.fromObject(map.get("map"));
        map =(Map) jsonObject ;
        System.out.println(map.get("name"));
        System.out.println(map.get("age"));
    }
    /*
     * jsonObject 中放jsonArray
     * 返回map类型的值
     * 用来查询后给前台返回数据
     * {"result":true,
     *  "data":[{"student":{"name":"liyong","age":23},"name":"caoke","age":12},
     *          {"student":null,"name":"liyang","age":32}
     *         ]
     * }
     * 
     */
    @Test
    public void jsonObjArry(){
        // 一个对象
        Person person1 = new Person();
        person1.setAge(12);
        person1.setName("caoke");
        // 对象中包含对象
        Student student = new Student();
        student.setAge(23);
        student.setName("liyong");
        person1.setStudent(student);
        // 一个对象
        Person person2 = new Person();
        person2.setAge(32);
        person2.setName("liyang");
        // 添加到集合
        List<Person> persons = new ArrayList<Person>();
        persons.add(person1);
        persons.add(person2);
        // 集合转换层jsonarray
        JSONArray jsonArray = JSONArray.fromObject(persons);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", true);
        jsonObject.put("data", jsonArray);
        System.out.println(jsonObject.toString());
        System.out.println(Json2Java.class.getResource("/"));
    }
    
}
 .net

相关文章
相关标签/搜索