fastjson转list bean

前端之家收集整理的这篇文章主要介绍了fastjson转list bean前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>

</dependency>



  1. publicclassFastJSON{
  2. /**
  3. *@paramargs
  4. */
  5. staticvoidmain(String[]args)throwsException{
  6. //string2Json();
  7. //string2Object();
  8. //string2List();
  9. map2json();
  10. map2JSON();
  11. }
  12. /**
  13. *通过fastjson把字符串转换成JSON数据
  14. *TypeReference
  15. voidstring2Json(){
  16. StringBufferbuffer=newStringBuffer();
  17. buffer.append("{");
  18. "\"age\":").append("27").append(",");
  19. "\"userName\":").append("\"龙叔\"").append();
  20. "\"address\":").append("\"广东省云浮市\"");
  21. "}");
  22. StringjsonText=buffer.toString();
  23. JSONObjectjobj=JSON.parSEObject(jsonText);
  24. Stringaddress=jobj.get("address").toString();
  25. System.out.println(address);
  26. *通过fastjson把字符串转换成对象
  27. voidstring2Object(){
  28. //方法一把json字符串转成Student对象
  29. Studentstu1=JSON.parSEObject(jsonText,newTypeReference<Student>(){});
  30. //方法二把json字符串转成Student对象
  31. Studentstu2=JSON.parSEObject(jsonText,Student.class);
  32. System.out.println(stu1.getAddress());
  33. System.out.println(stu2.getAddress());
  34. }
  35. *通过fastjson把字符串转换成泛型数组
  36. voidstring2List(){
  37. "[{");
  38. "}]");
  39. //转成成数组
  40. Student[]stu2=JSON.parSEObject(jsonText,255); background-color:inherit; font-weight:bold">newTypeReference<Student[]>(){});
  41. List<Student>list=Arrays.asList(stu2);
  42. for(Studentst:list){
  43. System.out.println(st.getAddress());
  44. //转换成ArrayList
  45. ArrayList<Student>list2=JSON.parSEObject(jsonText,255); background-color:inherit; font-weight:bold">newTypeReference<ArrayList<Student>>(){});
  46. for(inti=0;i<list2.size();i++){
  47. Studentobj=(Student)list2.get(i);
  48. System.out.println(obj.getAddress());
  49. *通过fastjson把Map换成字符串转
  50. */
  51. voidmap2json(){
  52. //创建一个Map对象
  53. Map<String,String>map=newHashMap<String,String>();
  54. map.put("username","周伯通");
  55. "address",0); background-color:inherit">"广东省仙游谷");
  56. "age",0); background-color:inherit">"198");
  57. Stringjson=JSON.toJSONString(map,255); background-color:inherit; font-weight:bold">true);//转成JSON数据
  58. //遍历数组数据
  59. for(Stringkey:map1.keySet()){
  60. System.out.println(key+":"+map1.get(key));
  61. *通过fastjson把Map换成字符串转
  62. voidmap2JSON(){
  63. Mapmap=newHashMap();
  64. Stringjson=JSON.toJSONString(map);
  65. Mapmap1=JSON.parSEObject(json);
  66. for(Objectobj:map.entrySet()){
  67. Map.Entry<String,String>entry=(Map.Entry<String,String>)obj;
  68. System.out.println(entry.getKey()+"--->"+entry.getValue());
  69. }


  1. packageivyy.taobao.com.entity;
  2. importjava.io.Serializable;
  3. *@Author:liangjl
  4. *@Date:2014-12-19
  5. *@Version:1.0
  6. *@Description:
  7. classStudentimplementsSerializable{
  8. privateIntegerage;
  9. privateStringsex;
  10. privateStringuserName;
  11. privateStringbirthday;
  12. privateStringaddress;
  13. privateStringemail;
  14. publicIntegergetAge(){
  15. returnage;
  16. voidsetAge(Integerage){
  17. this.age=age;
  18. publicStringgetSex(){
  19. returnsex;
  20. voidsetSex(Stringsex){
  21. this.sex=sex;
  22. publicStringgetUserName(){
  23. returnuserName;
  24. voidsetUserName(StringuserName){
  25. this.userName=userName;
  26. publicStringgetBirthday(){
  27. returnbirthday;
  28. voidsetBirthday(Stringbirthday){
  29. this.birthday=birthday;
  30. publicStringgetAddress(){
  31. returnaddress;
  32. voidsetAddress(Stringaddress){
  33. this.address=address;
  34. publicStringgetEmail(){
  35. returnemail;
  36. voidsetEmail(Stringemail){
  37. this.email=email;
  38. }

猜你在找的Json相关文章