Json,JsonObject,JsonArray,Bean

概念

  1. String:这个很好解释,指使用“”双引号或’’单引号包括的字符。例如:
    String comStr = "this is string; "
  2. Json:指的是符合json格式要求的字符串。例如:
    String jsonStr = "{StudentID:'100',Name:'tmac',Hometown:'usa'}";
  3. JSONObject:指符合json格式要求的对象,可以看做一个Map<String,Object>。例如:
    JSONObject jsonObj = { StudentID: "100", Name: "tmac", Hometown: "usa" };
  4. JSONArray:当做一个List,可以把JSONArray看成JSONObject对象的一个集合

Json字符串

    //json字符串-简单对象型
	private static final String  JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
	//json字符串-数组类型
	private static final String  JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
	//复杂格式json字符串
	private static final String  COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";

json字符串---->JSONObject
    /**
     * json字符串-简单对象型与JSONObject之间的转换
     */
	@Test
    public void testJSONStrToJSONObject(){
		
        JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
        //JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //因为JSONObject继承了JSON,所以这样也是可以的
 
        System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
        String jsonStr = JSONObject.toJSONString(jsonObject);
        System.out.println(jsonStr);
    }
json字符串---->JSONArray
    /**
     * json字符串-数组类型与JSONArray之间的转换
     */
	@Test
    public  void testJSONStrToJSONArray(){
 
        JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
        //JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因为JSONArray继承了JSON,所以这样也是可以的
 
        //遍历方式1
        int size = jsonArray.size();
        for (int i = 0; i < size; i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
        }
        //遍历方式2
        for (Object obj : jsonArray) {
            JSONObject jsonObject = (JSONObject) obj;
            System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
        }
    }
json字符串---->JSONObject【非重点】
    /**
     * 复杂json格式字符串与JSONObject之间的转换
     */
	@Test
    public void testComplexJSONStrToJSONObject(){
 
        JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
        //JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因为JSONObject继承了JSON,所以这样也是可以的
        
        String teacherName = jsonObject.getString("teacherName");
        Integer teacherAge = jsonObject.getInteger("teacherAge");
        JSONObject course = jsonObject.getJSONObject("course");
        JSONArray students = jsonObject.getJSONArray("students");
        
        Student s = new Student();
        s.setStudentName("王五");
        s.setStudentAge(12);
        JSONObject obj = (JSONObject) JSON.toJSON(s);
        
        //System.out.println(obj.toJSONString());
        Map smap = JSONObject.parseObject(obj.toJSONString(), Map.class);
        //System.out.println(smap);
        
        
        String str=JSONObject.toJSONString((Object)s);
        Map smap2 = JSONObject.parseObject(str, Map.class);
        //System.out.println(smap2);
        
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("studentName", "张三");
        map.put("studentAge", 20);
        
        String strmap=JSONObject.toJSONString((Object)map);
        Student stu = JSONObject.parseObject(strmap, Student.class);
        System.out.println(stu);
        
    }
json字符串---->Bean 【重点】
 /**
     * json字符串-简单对象与JavaBean_obj之间的转换[重点]
     */
	@Test
    public  void testJSONStrToJavaBeanObj(){
 
        Student student = JSON.parseObject(JSON_OBJ_STR, Student.class);
        //Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});//因为JSONObject继承了JSON,所以这样也是可以的
 
        System.out.println(student.getStudentName()+":"+student.getStudentAge());
 
    }
json字符串---->List【重点】
	/**
     * json字符串-数组类型与JavaBean_List之间的转换
     */
	@Test
    public void testJSONStrToJavaBeanList(){
        
        ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});
        //ArrayList<Student> students1 = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});//因为JSONArray继承了JSON,所以这样也是可以的
        
        for (Student student : students) {
            System.out.println(student.getStudentName()+":"+student.getStudentAge());
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值