fastjson使用实例

前端之家收集整理的这篇文章主要介绍了fastjson使用实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转自:http://www.jb51.cc/article/p-vcsszekb-yo.html

[java] view plain copy
  1. packagejunit.test;
  2. importjava.util.ArrayList;
  3. importjava.util.HashMap;
  4. importjava.util.List;
  5. importjava.util.Map;
  6. importcom.alibaba.fastjson.JSON;
  7. importcom.alibaba.fastjson.TypeReference;
  8. /*
  9. *这里将json的转化和解析都放在一起了,大家可以根据实际需要来转化json字符串和解析json字符串
  10. */
  11. publicclassTestFastJson{
  12. staticclassPerson{
  13. privateStringid;
  14. privateStringname;
  15. privateintage;
  16. publicPerson(){
  17. }
  18. publicPerson(Stringid,Stringname,intage){
  19. this.id=id;
  20. this.name=name;
  21. this.age=age;
  22. }
  23. publicStringgetId(){
  24. returnid;
  25. voidsetId(Stringid){
  26. publicStringgetName(){
  27. returnname;
  28. voidsetName(Stringname){
  29. this.name=name;
  30. intgetAge(){
  31. returnage;
  32. voidsetAge( @Override
  33. publicStringtoString(){
  34. return"Person[age="+age+",id="+id+",name="+name+"]";
  35. voidmain(String[]args){
  36. method1();
  37. method2();
  38. method3();
  39. method4();
  40. voidmethod1(){
  41. System.out.println("javabean转化示例开始----------");
  42. Personperson=newPerson("1","fastjson",1);
  43. //这里将javabean转化成json字符串
  44. StringjsonString=JSON.toJSONString(person);
  45. System.out.println(jsonString);
  46. //这里将json字符串转化成javabean对象,
  47. person=JSON.parSEObject(jsonString,Person.class);
  48. System.out.println(person.toString());
  49. System.out.println("javabean转化示例结束----------");
  50. voidmethod2(){
  51. System.out.println("List<javabean>转化示例开始----------");
  52. Personperson1="fastjson1",0); background-color:inherit">1);
  53. Personperson2=newPerson("2","fastjson2",0); background-color:inherit">2);
  54. List<Person>persons=newArrayList<Person>();
  55. persons.add(person1);
  56. persons.add(person2);
  57. StringjsonString=JSON.toJSONString(persons);
  58. System.out.println("json字符串:"+jsonString);
  59. //解析json字符串
  60. List<Person>persons2=JSON.parseArray(jsonString,0); background-color:inherit">//输出解析后的person对象,也可以通过调试模式查看persons2的结构
  61. System.out.println("person1对象:"+persons2.get(0).toString());
  62. System.out.println("person2对象:"+persons2.get(1).toString());
  63. System.out.println("List<javabean>转化示例结束----------");
  64. voidmethod3(){
  65. System.out.println("List<String>转化示例开始----------");
  66. List<String>list=newArrayList<String>();
  67. list.add("fastjson1");
  68. list.add("fastjson2");
  69. list.add("fastjson3");
  70. StringjsonString=JSON.toJSONString(list);
  71. List<String>list2=JSON.parSEObject(jsonString,newTypeReference<List<String>>(){});
  72. System.out.println(list2.get(0)+"::"+list2.get(1)+"::"+list2.get(2));
  73. System.out.println("List<String>转化示例结束----------");
  74. voidmethod4(){
  75. System.out.println("List<Map<String,Object>>转化示例开始----------");
  76. Map<String,Object>map=newHashMap<String,Object>();
  77. map.put("key1","value1");
  78. map.put("key2","value2");
  79. 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);
  80. List<Map<String,Object>>list=newArrayList<Map<String,Object>>();
  81. list.add(map);
  82. list.add(map2);
  83. StringjsonString=JSON.toJSONString(list);
  84. System.out.println("json字符串:"+jsonString);
  85. newTypeReference<List<Map<String,Object>>>(){});
  86. System.out.println("map的key1值"+list2.get(0).get("key1"));
  87. System.out.println("map的key2值"+list2.get(0).get("key2"));
  88. System.out.println("ma2p的key1值"+list2.get(1).get("key1"));
  89. System.out.println("map2的key2值"+list2.get(1).get("key2"));
  90. }
原文链接:https://www.f2er.com/json/288968.html

猜你在找的Json相关文章