欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fastjson例子  

程序员文章站 2022-03-28 21:04:33
...
1.前言.
  如题
2.例子.
import com.alibaba.fastjson.JSON;


public class Person {
	int age;
	String name;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public static void main(String[] args) {
		String jsonString="{\"name\":\"hah\",\"age\":1}";
		Person person = JSON.parseObject(jsonString, Person.class);
		System.out.println(1);
	}

}


3.参考.
上面为本人的例子,下面是摘录自博客园这位叫PointNet的博主的比较完整的文章:
http://www.cnblogs.com/goody9807/p/4244862.html
二. fastjson 解析json字符串为四种类型

      1. JavaBean

          Person person = JSON.parseObject(jsonString, Person.class);

      2. List<JavaBean>

          List<Person> listPerson =JSON.parseArray(jsonString, Person.class);

      3. List<String>

          List<String> listString = JSON.parseArray(jsonString, String.class);

      4. List<Map<String,Object>>

          List<Map<String, Object>> listMap = JSON.parseObject(jsonString, new TypeReference<List<Map<String,Object>>>(){});  (注意:此处可看出fastjson反射机制比gson更准确, id = 1001 通过fastjson反射仍是 id = 1001 , 而通过gson反射结果 为 id =1001.0 ,

 

JSON解析器fastjson(阿里巴巴出品,版本1.1.26),如果定义了JSONObject为{"JACKIE_ZHANG":"张学友","ANDY_LAU":"刘德华","LIMING":"黎明","Aaron_Kwok":"郭富城"},那么读取值时,KEY顺序无序,测试代码:

 

    import com.alibaba.fastjson.JSONObject;  
      
      
    /** 
     * Created by wangzhenfei on 14-4-15. 
     */  
    public class FastJsonTest {  
        public static void main(String[] args){  
            String jsonStr = "{\"JACKIE_ZHANG\":\"张学友\",\"ANDY_LAU\":\"刘德华\",\"LIMING\":\"黎明\",\"Aaron_Kwok\":\"郭富城\"}" ;  
      
      
            //做5次测试  
            for(int i=0,j=5;i<j;i++)  
            {  
               JSONObject jsonObject = JSONObject.parseObject(jsonStr) ;  
               for(java.util.Map.Entry<String,Object> entry:jsonObject.entrySet()){  
                   System.out.print(entry.getKey()+"-"+entry.getValue()+"\t");  
               }  
                System.out.println();//用来换行  
            }  
        }  
    }  


运行结果:

 

    LIMING-黎明 Aaron_Kwok-郭富城JACKIE_ZHANG-张学友ANDY_LAU-刘德华  
    Aaron_Kwok-郭富城 ANDY_LAU-刘德华LIMING-黎明JACKIE_ZHANG-张学友  
    Aaron_Kwok-郭富城 JACKIE_ZHANG-张学友ANDY_LAU-刘德华LIMING-黎明  
    LIMING-黎明 ANDY_LAU-刘德华JACKIE_ZHANG-张学友Aaron_Kwok-郭富城  
    JACKIE_ZHANG-张学友 LIMING-黎明ANDY_LAU-刘德华Aaron_Kwok-郭富城  

 

 

解决办法:定义为JSONArray,代码如下:

 

    import com.alibaba.fastjson.JSONArray;  
      
    /** 
     * Created by wangzhenfei on 14-4-15. 
     */  
    public class FastJsonTest {  
        public static void main(String[] args){  
            String jsonStr = "[{\"JACKIE_ZHANG\":\"张学友\"},{\"ANDY_LAU\":\"刘德华\"},{\"LIMING\":\"黎明\"},{\"Aaron_Kwok\":\"郭富城\"}]" ;  
            //做5次测试  
            for(int i=0,j=5;i<j;i++)  
            {  
                JSONArray jsonArray = JSONArray.parseArray(jsonStr);  
      
               for(int k=0;k<jsonArray.size();k++){  
                   System.out.print(jsonArray.get(k) + "\t");  
               }  
                System.out.println();//用来换行  
            }  
        }  
    }  


运行结果为:

 

    {"JACKIE_ZHANG":"张学友"} {"ANDY_LAU":"刘德华"}{"LIMING":"黎明"}{"Aaron_Kwok":"郭富城"}  
    {"JACKIE_ZHANG":"张学友"} {"ANDY_LAU":"刘德华"}{"LIMING":"黎明"}{"Aaron_Kwok":"郭富城"}  
    {"JACKIE_ZHANG":"张学友"} {"ANDY_LAU":"刘德华"}{"LIMING":"黎明"}{"Aaron_Kwok":"郭富城"}  
    {"JACKIE_ZHANG":"张学友"} {"ANDY_LAU":"刘德华"}{"LIMING":"黎明"}{"Aaron_Kwok":"郭富城"}  
    {"JACKIE_ZHANG":"张学友"} {"ANDY_LAU":"刘德华"}{"LIMING":"黎明"}{"Aaron_Kwok":"郭富城"}  

 

 

如果就想要定义为JSONObject,而不是JSONArray,可以选用其他JSON解析器,个人推荐使用google的gson,文档明显比fastjson好很多(从这里可以看出阿里巴巴和谷歌的差距):

 

    import com.google.gson.JsonElement;  
    import com.google.gson.JsonObject;  
    import com.google.gson.JsonParser;  
      
    /** 
     * Created by wangzhenfei on 14-4-15. 
     */  
    public class FastJsonTest {  
        public static void main(String[] args){  
            String jsonStr = "{\"JACKIE_ZHANG\":\"张学友\",\"ANDY_LAU\":\"刘德华\",\"LIMING\":\"黎明\",\"Aaron_Kwok\":\"郭富城\"}" ;  
            //做5次测试  
            for(int i=0,j=5;i<j;i++)  
            {  
                JsonObject jsonObject = (JsonObject) new JsonParser().parse(jsonStr);  
                for(java.util.Map.Entry<String,JsonElement> entry:jsonObject.entrySet()){  
                    System.out.print(entry.getKey()+"-"+entry.getValue()+"\t");  
                }  
                System.out.println();//用来换行  
            }  
        }  
    }  


运行结果:

 

 

        JACKIE_ZHANG-"张学友"  ANDY_LAU-"刘德华"  LIMING-"黎明" Aaron_Kwok-"郭富城"      
        JACKIE_ZHANG-"张学友"  ANDY_LAU-"刘德华"  LIMING-"黎明" Aaron_Kwok-"郭富城"      
        JACKIE_ZHANG-"张学友"  ANDY_LAU-"刘德华"  LIMING-"黎明" Aaron_Kwok-"郭富城"      
        JACKIE_ZHANG-"张学友"  ANDY_LAU-"刘德华"  LIMING-"黎明" Aaron_Kwok-"郭富城"      
        JACKIE_ZHANG-"张学友"  ANDY_LAU-"刘德华"  LIMING-"黎明" Aaron_Kwok-"郭富城"