fastjson使用实例

页面导航:首页 > 网络编程 > JavaScript > fastjson使用实例

fastjson使用实例

来源: 作者: 时间:2016-01-23 10:23 【

package junit test; import java util ArrayList; import java util HashMap; import java util List; import java util Map; import com alibaba fastjson JSON; i
  1. package junit.test;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.TypeReference;
  10. /*
  11. * 这里将json的转化和解析都放在一起了,大家可以根据实际需要来转化json字符串和解析json字符串
  12. */
  13. public class TestFastJson {
  14.  
  15. static class Person{
  16. private String id ;
  17. private String name;
  18. private int age ;
  19.  
  20. public Person(){
  21.  
  22. }
  23. public Person(String id,String name,int age){
  24. this.id=id;
  25. this.name=name;
  26. this.age=age;
  27. }
  28. public String getId() {
  29. return id;
  30. }
  31. public void setId(String id) {
  32. this.id = id;
  33. }
  34. public String getName() {
  35. return name;
  36. }
  37. public void setName(String name) {
  38. this.name = name;
  39. }
  40. public int getAge() {
  41. return age;
  42. }
  43. public void setAge(int age) {
  44. this.age = age;
  45. }
  46. @Override
  47. public String toString() {
  48. return "Person [age=" + age + ", id=" + id + ", name=" + name + "]";
  49. }
  50.  
  51.  
  52. }
  53. public static void main(String[] args) {
  54. method1();
  55. method2();
  56. method3();
  57. method4();
  58. }
  59.  
  60. static void method1(){
  61. System.out.println("javabean转化示例开始----------");
  62. Person person = new Person("1","fastjson",1);
  63.  
  64. //这里将javabean转化成json字符串
  65. String jsonString = JSON.toJSONString(person);
  66. System.out.println(jsonString);
  67. //这里将json字符串转化成javabean对象,
  68. person =JSON.parseObject(jsonString,Person.class);
  69. System.out.println(person.toString());
  70.  
  71. System.out.println("javabean转化示例结束----------");
  72. }
  73.  
  74. static void method2(){
  75. System.out.println("List转化示例开始----------");
  76.  
  77. Person person1 = new Person("1","fastjson1",1);
  78. Person person2 = new Person("2","fastjson2",2);
  79. List persons = new ArrayList();
  80. persons.add(person1);
  81. persons.add(person2);
  82. String jsonString = JSON.toJSONString(persons);
  83. System.out.println("json字符串:"+jsonString);
  84.  
  85. //解析json字符串
  86. List persons2 = JSON.parseArray(jsonString,Person.class);
  87. //输出解析后的person对象,也可以通过调试模式查看persons2的结构
  88. System.out.println("person1对象:"+persons2.get(0).toString());
  89. System.out.println("person2对象:"+persons2.get(1).toString());
  90.  
  91. System.out.println("List转化示例结束----------");
  92. }
  93.  
  94. static void method3(){
  95. System.out.println("List转化示例开始----------");
  96. List list = new ArrayList();
  97. list.add("fastjson1");
  98. list.add("fastjson2");
  99. list.add("fastjson3");
  100. String jsonString = JSON.toJSONString(list);
  101. System.out.println("json字符串:"+jsonString);
  102.  
  103. //解析json字符串
  104. List list2 = JSON.parseObject(jsonString,new TypeReference>(){});
  105. System.out.println(list2.get(0)+"::"+list2.get(1)+"::"+list2.get(2));
  106. System.out.println("List转化示例结束----------");
  107.  
  108. }
  109.  
  110. static void method4(){
  111. System.out.println(" List>转化示例开始 ----------");
  112. Map map = new HashMap();
  113. map.put("key1", "value1");
  114. map.put("key2", "value2");
  115. Map map2 = new HashMap();
  116. map2.put("key1", 1);
  117. map2.put("key2", 2);
  118. List> list = new ArrayList>();
  119. list.add(map);
  120. list.add(map2);
  121. String jsonString = JSON.toJSONString(list);
  122. System.out.println("json字符串:"+jsonString);
  123. //解析json字符串
  124. List> list2 = JSON.parseObject(jsonString,new TypeReference>>(){});
  125.  
  126. System.out.println("map的key1值"+list2.get(0).get("key1"));
  127. System.out.println("map的key2值"+list2.get(0).get("key2"));
  128. System.out.println("ma2p的key1值"+list2.get(1).get("key1"));
  129. System.out.println("map2的key2值"+list2.get(1).get("key2"));
  130. }
  131.  
  132. }
Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<