FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换

fastJson对于json格式字符串的解析主要用到了一下三个类:

JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。

JSONObject:fastJson提供的json对象。

JSONArray:fastJson提供json数组对象。

我们可以把JSONObject当成一个Map<String,Object>来看,只是JSONObject提供了更为丰富便捷的方法,方便我们对于对象属性的操作。我们看一下源码。

同样我们可以把JSONArray当做一个List<Object>,可以把JSONArray看成JSONObject对象的一个集合。

此外,由于JSONObject和JSONArray继承了JSON,所以说也可以直接使用两者对JSON格式字符串与JSON对象及javaBean之间做转换,不过为了避免混淆我们还是使用JSON。

首先定义三个json格式的字符串,作为我们的数据源。

1Mapjson转换

首先要导入一个fastjson-1.2.38.jar  jar包,这个包具有快速转换格式的作用

map都是以键值的方式存储。

(1)map键可以使interger也可以是String两个参数

(2)mapsize,键和值是代表一条数据

(3)map.clear)清除数据,使用这个后面map就会被清空

(4)map.keySet()这个是代表的是mapkey的集合。将所有mapkey值放在一起了。

   map.values() 得到所有的map值的集合

   map.get 得到每个key对应的value

代码如下:

package com.test;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
public class testJson {
/*
 * 主要将map存的值进行json,必须有json jar包的前提下
 * */
public static void main(String[] args) {

/*
	 * 1、map 转换json 
	 */
	 System.out.println("map转换json");
	 Map map=new HashMap();
	 map.put("plan_code","1010");
	 map.put("plan_type","H");
	 map.put("sum_ins","10000");
	 String mapToJSON = JSONObject.toJSONString(map);
	 System.out.println(mapToJSON);
	 //输出结果:{"sum_ins":"10000","plan_code":"1010","plan_type":"H"}

}

2listjson转换

 List <Userpojo> list=new ArrayList<Userpojo>();
		 Userpojo  u=new Userpojo();
	       u.setId(1);
	       u.setUsername("刘佩佩");
	       u.setPassword("111111");
	       u.setEmail("1101721@qq.com");
	       Userpojo  u1=new Userpojo();
	       u1.setId(2);
	       u1.setUsername("刘志豪");
	       u1.setPassword("222222");
	       u1.setEmail("123455@qq.com");
	       list.add(u1);
	       list.add(u);
		   System.out.println("list的类型:"+list); 
		   //方式1
		   String list_tmap = JSONObject.toJSONString(list);
		   System.out.println("jsonObjet:"+list_tmap);
		   
		   
		   //方式2
		   String list_to_json=JSONArray.toJSONString(list);
		   System.out.println("JSONArray:"+list_to_json);

 //输出结果:[{"email":"123455@qq.com","id":2,"password":"222222","username":"刘志豪"},{"email":"1101721@qq.com","id":1,"password":"111111","username":"刘佩佩"}]

3json格式字符串与json 对象转换

 //json字符串-简单对象型
final static String  JSON_OBJ_STR = "{\"stu_name\":\"peipei\",\"age\":24}";
   
public static void testJSONStrToJSONObject(){
        JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
      //  JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //因为JSONObject继承了JSON,所以这样也是可以的
        System.out.println(jsonObject1.getString("studentName")+":"+jsonObject.getInteger("studentAge"));

    }

输出的结果:  peipei:24

4、复杂json格式字符串与JSONObject之间的转换

//json字符串-数组类型 
  final static String  COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";

 JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
 //JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因为JSONObject继承了JSON,所以这样也是可以的
	        System.out.println(jsonObject);
	        String teacherName = jsonObject.getString("teacherName");
	        Integer teacherAge = jsonObject.getInteger("teacherAge");
	        JSONObject course = jsonObject.getJSONObject("course");
	        JSONArray students = jsonObject.getJSONArray("students");
             System.out.println("teacherName:"+teacherName+"\t"+"teacherAge:"+teacherAge+"\t"+"course:"+course+"\t"+"students:"+students+"\t");

输出的结果:    

         teacherName:crystall        

         teacherAge:27        

            course:{"code":1270,"courseName":"english"}        students:[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]        

5json字符串转java代码

 String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}";
		 
	             JSONObject  jsonObj = JSON.parseObject(jsonStr);
		   String username = jsonObj.getString("username");
		   String password = jsonObj.getString("password");
		   System.out.println("json--->java\n username=" + username
		   + "\t password=" + password);

输出的结果:username=张三         password=123456

6、json字符串-简单对象型与javaBean之间的转换

对于 TypeReference<T> ,由于其构造方法使用 protected 进行修饰,所以在其他包下创建其对象的时候,要用其实现类的子类: new TypeReference<Teacher>() {}
StudentPojo student = JSON.parseObject(JSON_OBJ_STR, new TypeReference<StudentPojo>() {});
        //Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});//因为JSONObject继承了JSON,所以这样也是可以的
        System.out.println(student.getStu_name()+":"+student.getAge());

输出的结果:peipei:24

7、json字符串-数组类型与JavaBean_List之间的转换


 public static void testJSONStrToJavaBeanList(){
        
 ArrayList<StudentPojo> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<StudentPojo>>() {});
        //ArrayList<Student> students1 = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});//因为JSONArray继承了JSON,所以这样也是可以的
        
        for (StudentPojo student : students) {
            System.out.println(student.getStu_name()+":"+student.getAge());
        }
    }

输出的结果:lily:12

        lucy:15


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值