转自:http://www.jb51.cc/article/p-vcsszekb-yo.html
- packagejunit.test;
- importjava.util.ArrayList;
- importjava.util.HashMap;
- importjava.util.List;
- importjava.util.Map;
- importcom.alibaba.fastjson.JSON;
- importcom.alibaba.fastjson.TypeReference;
- /*
- *这里将json的转化和解析都放在一起了,大家可以根据实际需要来转化json字符串和解析json字符串
- */
- publicclassTestFastJson{
- staticclassPerson{
- privateStringid;
- privateStringname;
- privateintage;
- publicPerson(){
- }
- publicPerson(Stringid,Stringname,intage){
- this.id=id;
- this.name=name;
- this.age=age;
- }
- publicStringgetId(){
- returnid;
- voidsetId(Stringid){
- publicStringgetName(){
- returnname;
- voidsetName(Stringname){
- this.name=name;
- intgetAge(){
- returnage;
- voidsetAge( @Override
- publicStringtoString(){
- return"Person[age="+age+",id="+id+",name="+name+"]";
- voidmain(String[]args){
- method1();
- method2();
- method3();
- method4();
- voidmethod1(){
- System.out.println("javabean转化示例开始----------");
- Personperson=newPerson("1","fastjson",1);
- //这里将javabean转化成json字符串
- StringjsonString=JSON.toJSONString(person);
- System.out.println(jsonString);
- //这里将json字符串转化成javabean对象,
- person=JSON.parSEObject(jsonString,Person.class);
- System.out.println(person.toString());
- System.out.println("javabean转化示例结束----------");
- voidmethod2(){
- System.out.println("List<javabean>转化示例开始----------");
- Personperson1="fastjson1",0); background-color:inherit">1);
- Personperson2=newPerson("2","fastjson2",0); background-color:inherit">2);
- List<Person>persons=newArrayList<Person>();
- persons.add(person1);
- persons.add(person2);
- StringjsonString=JSON.toJSONString(persons);
- System.out.println("json字符串:"+jsonString);
- //解析json字符串
- List<Person>persons2=JSON.parseArray(jsonString,0); background-color:inherit">//输出解析后的person对象,也可以通过调试模式查看persons2的结构
- System.out.println("person1对象:"+persons2.get(0).toString());
- System.out.println("person2对象:"+persons2.get(1).toString());
- System.out.println("List<javabean>转化示例结束----------");
- voidmethod3(){
- System.out.println("List<String>转化示例开始----------");
- List<String>list=newArrayList<String>();
- list.add("fastjson1");
- list.add("fastjson2");
- list.add("fastjson3");
- StringjsonString=JSON.toJSONString(list);
- List<String>list2=JSON.parSEObject(jsonString,newTypeReference<List<String>>(){});
- System.out.println(list2.get(0)+"::"+list2.get(1)+"::"+list2.get(2));
- System.out.println("List<String>转化示例结束----------");
- voidmethod4(){
- System.out.println("List<Map<String,Object>>转化示例开始----------");
- Map<String,Object>map=newHashMap<String,Object>();
- map.put("key1","value1");
- map.put("key2","value2");
- Map<String,Object>map2= map2.put("key1",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> map2.put("key2",0); background-color:inherit">2);
- List<Map<String,Object>>list=newArrayList<Map<String,Object>>();
- list.add(map);
- list.add(map2);
- StringjsonString=JSON.toJSONString(list);
- System.out.println("json字符串:"+jsonString);
- newTypeReference<List<Map<String,Object>>>(){});
- System.out.println("map的key1值"+list2.get(0).get("key1"));
- System.out.println("map的key2值"+list2.get(0).get("key2"));
- System.out.println("ma2p的key1值"+list2.get(1).get("key1"));
- System.out.println("map2的key2值"+list2.get(1).get("key2"));
- }