点击上面 免费订阅本账号!
本公众号主要推送javaweb开发相关技术,基础知识点,同时会深入剖析复杂的问题,分享一些优秀的框架,大型项目经验,当今最流行的Javaweb技术,热点科技新闻,招聘信息,生活乐趣等等。点击上方的蓝字,这样您每天可以看到更多的java知识和资讯!完全是免费订阅,请放心关注。
测试代码:
[java] view plain copy
1. package com.ppl.jsonconvert;
2.
3. import java.util.ArrayList;
4. import java.util.HashMap;
5. import java.util.LinkedHashMap;
6. import java.util.List;
7. import java.util.Map;
8.
9. import com.ppl.json.commons.JsonUtils;
10.
11.import net.sf.json.JSONArray;
12.import net.sf.json.JSONObject;
13.
14.public class Test {
15.
16. @SuppressWarnings({ "unused", "rawtypes", "unchecked" })
17. public static void main(String[] args) {
18.
19. ///******************************
20. //普通java实体类转化为JSON字符串形式
21. ///******************************
22. Student stu=new Student();
23. stu.setName("JSON");
24. stu.setAge("23");
25. stu.setAddress("北京市西城区");
26.
27. //1、使用JSONObject
28. JSONObject jsonObject = JSONObject.fromObject(stu);
29. String strJson=jsonObject.toString();
30.
31. //strJson:{"address":"北京市西城区","age":"23","name":"JSON"}
32. System.out.println("strJson:"+strJson);
33.
34. //2、使用JSONArray
35. JSONArray jsonArray=JSONArray.fromObject(stu);
36. String strArray=jsonArray.toString();
37.
38. //strArray:[{"address":"北京市西城区","age":"23","name":"JSON"}]
39. System.out.println("strArray:"+strArray);
40. //
41.
42.
43. ///******************************
44. //JSON字符串--》》java对象
45. ///******************************
46.
47. //定义两种不同格式的字符串
48. String objectStr="{\"name\":\"JSONStu1\",\"age\":\"18\",\"address\":\"北京市西城区\"}";
49. String arrayStr="[{\"name\":\"JSONStu2\",\"age\":\"22\",\"address\":\"济南市历城区\"}]";
50. //1、使用JSONObject--将文字列转换为JSON
51.
52. JSONObject jsonObject2=JSONObject.fromObject(objectStr);
53. Student stu2=(Student)JSONObject.toBean(jsonObject2, Student.class);
54. System.out.println("stu2:"+stu2);
55.
56. //2、使用JSONArray--将文字列转换为JSON
57. JSONArray jsonArray2=JSONArray.fromObject(arrayStr);
58. //获得jsonArray的第一个元素
59. Object o=jsonArray2.get(0);
60. JSONObject jsonObject3=JSONObject.fromObject(o);
61. Student stu3=(Student)JSONObject.toBean(jsonObject3, Student.class);
62.
63. System.out.println("stu3:"+stu3);
64.
65.
66. ///******************************
67. //Map转换成json, 大多使用jsonObject(注1)
68. ///******************************
69. //不建议使用出自于 org.apache.commons.collections.OrderedMap
70. //Map<String, Integer> ordermap = (Map<String, Integer>)new ListOrderedMap();
71. //有序的Map
72. Map ordermap = new LinkedHashMap<String, String>();
73.
74. Map map = new HashMap<String,Object>();
75. map.put( "name", "json" );
76. map.put( "bool", Boolean.TRUE );
77. map.put( "int", new Integer(1) );
78. map.put( "arr", new String[]{"a","b"} );
79. map.put( "func", "function(i){ return this.arr[i]; }" );
80.
81. JSONObject jsonObjectMap = JSONObject.fromObject( map );
82. System.out.println( "Map转换成json:"+jsonObjectMap );
83.
84. //注1示例详解
85. Student stu4=new Student("JSON","28","上海");
86.
87. Map<String,Student> map4=new HashMap<String,Student>();
88. map4.put("first", stu4);
89. //1、JSONObject
90. JSONObject mapObject=JSONObject.fromObject(map4);
91. System.out.println("mapObject"+mapObject.toString());
92. //2、JSONArray
93. JSONArray mapArray=JSONArray.fromObject(map4);
94. System.out.println("mapArray:"+mapArray.toString());
95.
96.
97. ///******************************
98. //list--》》json字符串,使用JSONArray
99. ///******************************
100. Student stu5=new Student("student1","16","jinan");
101. Student stu6=new Student("student2","26","qingdao");
102. List<Student> lists=new ArrayList<Student>();
103. lists.add(stu5);
104. lists.add(stu6);
105. //1、 使用JSONArray
106. JSONArray listArray=JSONArray.fromObject(lists);
107.
108. System.out.println("listArray:"+listArray.toString());
109.
110. ///******************************
111. //json字符串--》》list
112. ///******************************
113. String arrayStr2="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"济南高新区\"}]";
114. //转化为list
115. List<Student> list2=(List<Student>)JSONArray.toList(JSONArray.fromObject(arrayStr2), Student.class);
116. for (Student stus : list2) {
117. System.out.println(stus);
118. }
119.
120. //最新的方法json字符串--》》list
121. JSONArray jsonArrayT = JSONArray.fromObject(arrayStr2);
122. List<Student> listData = (List) JSONArray.toCollection(jsonArrayT, Student.class);
123. for (Student stus : listData) {
124. System.out.println("listData: "+stus);
125. }
126. //转化为数组
127. Student[] ss =(Student[])JSONArray.toArray(JSONArray.fromObject(arrayStr),Student.class);
128. for (Student student : ss) {
129. System.out.println(student);
130.
131. }
132.
133.
134. ///******************************
135. //json字符串--》》Map
136. ///******************************
137.
138. //BUG___________________TODO----------BUG
139. String strObject="{\"KEY\":{\"address\":\"中国上海\",\"age\":\"23\",\"name\":\"JSON\"}}";
140. Map<String, String> dataMap = JsonUtils.toHashMap(strObject);
141. System.out.println(dataMap.toString());
142. }
143.
144.}
点击阅读全文查看"美女为啥没IT男追"
有人用微信聊天,有人却在微信中学习,成长。下面是2016最HOT IT公众号,赶快试试新的关注方法吧!
关注方式
★长按二维码,选择“识别图中二维码”进行关注。