FastJson 的使用

FastJson 的使用


FastJSOn是阿里巴巴开源的JSON处理工具,包括“序列化”和“反序列化”两部分,它具备如下特征:

  1. 速度最快,测试表明,fastjson具有极快的性能,超越任其他的java json parser。包括自称最快的jackson。
  2. 功能强大,完全支持java bean、集合、Map、日期、Enum,支持范型,支持自省。
  3. 无依赖,能够直接运行在Java SE 5.0以上版本
  4. 支持Android。
  5. 开源 (Apache 2.0)

既然速度快,当然要体验一下。更何况是国产货,当然要支持了。测试一下,以后在项目里使用...

要使用 JSON. 要引入fastjson 包

代码

package com.json.fastjson;


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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.json.bean.Person;
/*
 *  利用 fastjson 进行转换和解析数据
 */
public class FastjsonClass {
	public static void main(String[] args) {
		//method1();
		//method2();
		//method3();
		method4();
	    //method5();
	    //method6();
	    method7();
	}
	
	static void method1(){
		System.out.println("将json字符串转化成JavaBean对象");
		Person person = new Person("1","fastjson",1);
		
		//将javaBean 对象
		String jsonString = JSON.toJSONString(person);
		System.out.println(jsonString);
		//将json字符串转化成JavaBean对象
		person =JSON.parSEObject(jsonString,Person.class);
		System.out.println(person.toString());
		
		System.out.println("将json字符串转化成JavaBean对象");
	}
	
	static void method2(){
		System.out.println("将json字符串转化成List<JavaBean>对象");
		
		Person person1 = new Person("1","fastjson1",1);
		Person person2 = new Person("2","fastjson2",2);
		List<Person> persons = new ArrayList<Person>();
		persons.add(person1);
		persons.add(person2);
		String jsonString = JSON.toJSONString(persons);
		System.out.println("将json字符串转化成List<JavaBean>对象"+jsonString);
		
		//进行json解析
		List<Person> persons2 = JSON.parseArray(jsonString,Person.class);
		
		System.out.println("person1解析后的值:"+persons2.get(0).toString());
		System.out.println("person2解析后的值:"+persons2.get(1).toString());
		
		System.out.println("将json字符串转化成List<JavaBean>对象完成");
	}
	
	static void method3(){
		System.out.println("将json字符串转化成List<String>对象");
		List<String> list = new ArrayList<String>();
		list.add("fastjson1");
		list.add("fastjson2");
		list.add("fastjson3");
		String jsonString = JSON.toJSONString(list);
		System.out.println("json转换后:"+jsonString);
		
		//解析json
		List<String> list2 = JSON.parSEObject(jsonString,new TypeReference<List<String>>(){}); 
		System.out.println(list2.get(0)+"::"+list2.get(1)+"::"+list2.get(2));
		System.out.println("将json字符串转化成List<String>对象成功");

	}
	
	static void method4(){
		System.out.println("将json字符串转化成List<Map<String,Object>>对象");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("key1","value1");
		map.put("key2","value2");
		Map<String,Object> map2 = new HashMap<String,Object>();
		map2.put("key1",1);
		map2.put("key2",2);
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		list.add(map);
		list.add(map2);
		String jsonString = JSON.toJSONString(list);
		System.out.println("json转化后:"+jsonString);
		//解析json数据
		List<Map<String,Object>> list2 = JSON.parSEObject(jsonString,new TypeReference<List<Map<String,Object>>>(){});
		System.out.println("List<Map>解析后: "+list2);
		System.out.println("map的key1值:ֵ"+list2.get(0).get("key1"));
		System.out.println("map的key2值:"+list2.get(0).get("key2"));
		System.out.println("map2的key1值:ֵ"+list2.get(1).get("key1"));
		System.out.println("map2的key2值:ֵ"+list2.get(1).get("key2"));
	}
	
	
	static void method5(){
        System.out.println("将json字符串转化成map(string,List<JavaBean>)对象");
        
        Person person1 = new Person("1",1);
        Person person2 = new Person("2",2);
        List<Person> persons = new ArrayList<Person>();
        persons.add(person1);
        persons.add(person2);
        String jsonString = JSON.toJSONString(persons);
        System.out.println("map(string,List<JavaBean>):"+jsonString);
        
        Map<String,String> map = new HashMap<String,String>(); 
        map.put("key1",jsonString);
        
        String json = JSON.toJSONString(map,true); 
        System.out.println(json); 
        for (String key : map.keySet()) { 
            //System.out.println(key+":"+map.get(key)); 
          //进行json解析
            List<Person> persons2 = JSON.parseArray(map.get(key),Person.class);
            System.out.println("person1解析后的值:"+persons2.get(0).toString());
            System.out.println("person2解析后的值:"+persons2.get(1).toString());
        } 
        
    }
	
	static void method6(){
	    Map<String,String>(); 
        map.put("a",new String("hhh")); 
        map.put("b",new String("aaa")); 
        map.put("c",new String("ggg")); 
        String json = JSON.toJSONString(map,true); 
        System.out.println(json); 
        //JSON -> Map 
        Map<String,String> map1 = (Map<String,String>)JSON.parse(json); 
        for (String key : map1.keySet()) { 
                System.out.println(key+":"+map1.get(key)); 
        } 
	}
	
	static void method7(){
        System.out.println("将json字符串转化成Map<string,List<Map<string,string>>对象");
        
        Map<String,"aaa");
        map.put("key2","bbb");
        map.put("key3","ccc");
        /*Person person1 = new Person("1",2);*/
        
        //String json = JSON.toJSONString(map,true); 
        Map<String,String> mapk = new HashMap<String,String>(); 
        mapk.put("key1","a1a");
        mapk.put("key2","b2b");
        mapk.put("key3","c3c");
        /*Person person1 = new Person("1",2);*/
        
        //String json1 = JSON.toJSONString(mapk,true); 
        List<Map<String,String>> persons = new ArrayList<Map<String,String>>();

        persons.add(map);
        persons.add(mapk);
        
        String jsonString = JSON.toJSONString(persons);
        System.out.println("List<Map>:"+jsonString);
        
        Map<String,String> map1 = new HashMap<String,String>(); 
        map1.put("table1",jsonString);
        map1.put("table2",jsonString);
        String jsonString1 = JSON.toJSONString(map1);
        
        System.out.println("map<List<Map>>:"+jsonString1);
        Map<String,String> map2 = (Map<String,String>)JSON.parse(jsonString1); 
        for (String key : map2.keySet()) { 
            //System.out.println(key+":"+map.get(key)); 
            //进行json解析
            String listparse = map2.get(key);
            System.out.println("List<Map>:"+listparse);
            List<Map<String,Object>> list2 = JSON.parSEObject(listparse,Object>>>(){}); 
            System.out.println("List<Map>转换后:"+list2);

            for(int i = 0; i < list2.size(); i++){
                //System.out.println("map的key1值:ֵ"+list2.get(i).get("key1"));
               
                for(String key1:list2.get(i).keySet()){
                    System.out.println(key1+":"+list2.get(i).get(key1));
                }
                
            }
           
        } 
        
    }
}



代码
package com.json.bean;

public class Person {
	private String id ;
	private String name;
	private int age ;
	
	public Person(){
		
	}
	public Person(String id,String name,int age){
		this.id=id;
		this.name=name;
		this.age=age;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
	    return "解析json字符串后获取的person对象:id的值是" + id + ",name的值是" + name + ",age的值是" + age + "";
	}
	
}

相关文章

  jsonp需要在页面中添加一个<script>元素,由该元素来从其他服务器加载json数据。 <body&g...
<script> var testApi = "地址"; $.ajax({ url:testApi,//可以不是本地域名 type:‘post...
总是有人会遇到跨域问题,然后有个jsonp的解决方案,MVC中代码如下: public class JsonpResult : Syst...
最近开发中遇到调用第三方web_api的功能,后端在处理json数据时使用fastjson来做反序列化,由于调用api...
JSON全称为JavaScript ObjectNotation,它是一种轻量级的数据交换格式,易于阅读、编写、解析。jsoncpp...
JsonSerializer有多个属性,用于自定义如何序列化JSON。这些也可以通过JsonSerializerSettings参数,在...