<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
- publicclassFastJSON{
- /**
- *@paramargs
- */
- staticvoidmain(String[]args)throwsException{
- //string2Json();
- //string2Object();
- //string2List();
- map2json();
- map2JSON();
- }
- /**
- *通过fastjson把字符串转换成JSON数据
- *TypeReference
- voidstring2Json(){
- StringBufferbuffer=newStringBuffer();
- buffer.append("{");
- "\"age\":").append("27").append(",");
- "\"userName\":").append("\"龙叔\"").append();
- "\"address\":").append("\"广东省云浮市\"");
- "}");
- StringjsonText=buffer.toString();
- JSONObjectjobj=JSON.parSEObject(jsonText);
- Stringaddress=jobj.get("address").toString();
- System.out.println(address);
- *通过fastjson把字符串转换成对象
- voidstring2Object(){
- //方法一把json字符串转成Student对象
- Studentstu1=JSON.parSEObject(jsonText,newTypeReference<Student>(){});
- //方法二把json字符串转成Student对象
- Studentstu2=JSON.parSEObject(jsonText,Student.class);
- System.out.println(stu1.getAddress());
- System.out.println(stu2.getAddress());
- }
- *通过fastjson把字符串转换成泛型数组
- voidstring2List(){
- "[{");
- "}]");
- //转成成数组
- Student[]stu2=JSON.parSEObject(jsonText,255); background-color:inherit; font-weight:bold">newTypeReference<Student[]>(){});
- List<Student>list=Arrays.asList(stu2);
- for(Studentst:list){
- System.out.println(st.getAddress());
- //转换成ArrayList
- ArrayList<Student>list2=JSON.parSEObject(jsonText,255); background-color:inherit; font-weight:bold">newTypeReference<ArrayList<Student>>(){});
- for(inti=0;i<list2.size();i++){
- Studentobj=(Student)list2.get(i);
- System.out.println(obj.getAddress());
- *通过fastjson把Map换成字符串转
- */
- voidmap2json(){
- //创建一个Map对象
- Map<String,String>map=newHashMap<String,String>();
- map.put("username","周伯通");
- "address",0); background-color:inherit">"广东省仙游谷");
- "age",0); background-color:inherit">"198");
- Stringjson=JSON.toJSONString(map,255); background-color:inherit; font-weight:bold">true);//转成JSON数据
- //遍历数组数据
- for(Stringkey:map1.keySet()){
- System.out.println(key+":"+map1.get(key));
- *通过fastjson把Map换成字符串转
- voidmap2JSON(){
- Mapmap=newHashMap();
- Stringjson=JSON.toJSONString(map);
- Mapmap1=JSON.parSEObject(json);
- for(Objectobj:map.entrySet()){
- Map.Entry<String,String>entry=(Map.Entry<String,String>)obj;
- System.out.println(entry.getKey()+"--->"+entry.getValue());
- }
- packageivyy.taobao.com.entity;
- importjava.io.Serializable;
- *@Author:liangjl
- *@Date:2014-12-19
- *@Version:1.0
- *@Description:
- classStudentimplementsSerializable{
- privateIntegerage;
- privateStringsex;
- privateStringuserName;
- privateStringbirthday;
- privateStringaddress;
- privateStringemail;
- publicIntegergetAge(){
- returnage;
- voidsetAge(Integerage){
- this.age=age;
- publicStringgetSex(){
- returnsex;
- voidsetSex(Stringsex){
- this.sex=sex;
- publicStringgetUserName(){
- returnuserName;
- voidsetUserName(StringuserName){
- this.userName=userName;
- publicStringgetBirthday(){
- returnbirthday;
- voidsetBirthday(Stringbirthday){
- this.birthday=birthday;
- publicStringgetAddress(){
- returnaddress;
- voidsetAddress(Stringaddress){
- this.address=address;
- publicStringgetEmail(){
- returnemail;
- voidsetEmail(Stringemail){
- this.email=email;
- }