(net.sf.json)java对象和json对象之间互相转换
01. import java.util.ArrayList;
02. import java.util.Collection;
03. import java.util.Iterator;
04. import java.util.List;
05.
06. import net.sf.json.JSONArray;
07. import net.sf.json.JSONObject;
08.
09. public class MainClass {
10.
11. public static void main(String[] args) {
12. TestJsonBean();
13. TestJsonAttribute();
14. TestJsonArray();
15. }
16.
17. @SuppressWarnings("rawtypes")
18. private static void TestJsonArray() {
19. Student student1 = new Student();
20. student1.setId(1);
21. student1.setName("jag");
22. student1.setSex("man");
23. student1.setAge(25);
24. student1.setHobby(new String[]{"篮球","游戏"});
25.
26. Student student2 = new Student();
27. student2.setId(2);
28. student2.setName("tom");
29. student2.setSex("woman");
30. student2.setAge(23);
31. student2.setHobby(new String[]{"上⽹","跑步"});
32.
33. List<Student> list = new ArrayList<Student>();
34. list.add(student1);
35. list.add(student2);
36.
37. JSONArray jsonArray = JSONArray.fromObject(list);
38. System.out.String());
39.
40. JSONArray new_jsonArray=JSONArray.Array());
41. Collection java_Collection(new_jsonArray);
42. if(java_collection!=null && !java_collection.isEmpty())
43. {
44. Iterator it=java_collection.iterator();
45. while(it.hasNext())
46. {
47. JSONObject jsonObj=JSONObject.());
48. Student stu=(Student) Bean(jsonObj,Student.class);
49. System.out.Name());
json转换对象50. }
51. }
52. }
53.
54. private static void TestJsonAttribute() {
55. /**
56. * 创建json对象并为该对象设置属性
57. */
58. JSONObject jsonObj = new JSONObject();
59. jsonObj.put("Int_att",25);//添加int型属性
60. jsonObj.put("String_att","str");//添加string型属性
61. jsonObj.put("Double_att",12.25);//添加double型属性
62. jsonObj.put("Boolean_att",true);//添加boolean型属性
63. //添加JSONObject型属性
64. JSONObject jsonObjSon = new JSONObject();
65. jsonObjSon.put("id", 1);
66. jsonObjSon.put("name", "tom");
67. jsonObj.put("JSONObject_att",jsonObjSon);
68. //添加JSONArray型属性
69. JSONArray jsonArray = new JSONArray();
70. jsonArray.add("array0");
71. jsonArray.add("array1");
72. jsonArray.add("array2");
73. jsonArray.add("array3");
74. jsonObj.put("JSONArray_att", jsonArray);
75. System.out.String());
76. System.out.println("Int_att:"+Int("Int_att"));
76. System.out.println("Int_att:"+Int("Int_att"));
77. System.out.println("String_att:"+String("String_att"));
78. System.out.println("Double_att:"+Double("Double_att"));
79. System.out.println("Boolean_att:"+Boolean("Boolean_att"));
80. System.out.println("JSONObject_att:"+JSONObject("JSONObject_att"));
81. System.out.println("JSONArray_att:"+JSONArray("JSONArray_att"));
82. }
83.
84. /**
85. * java对象与json对象互相转换
86. */
87. private static void TestJsonBean() {
88. /**
89. * 创建java对象
90. */
91. Student student = new Student();
92. student.setId(1);
93. student.setName("jag");
94. student.setSex("man");
95. student.setAge(25);
96. student.setHobby(new String[]{"篮球","上⽹","跑步","游戏"});
97. /**
98. * java对象转换成json对象,并获取json对象属性
99. */
100. JSONObject jsonStu = JSONObject.fromObject(student);
101. System.out.String());
102. System.out.JSONArray("hobby"));
103. /**
104. * json对象转换成java对象,并获取java对象属性
105. */
106. Student stu = (Student) Bean(jsonStu, Student.class);
107. System.out.Name());
108. /**
109. * 创建json对象
110. */
111. JSONObject jsonObj = new JSONObject();
112. jsonObj.put("id",1);
113. jsonObj.put("name","张勇");
114. jsonObj.put("sex","男");
115. jsonObj.put("age",24);
116. //jsonObj.put("hobby",new String[]{"上⽹","游戏","跑步","⾳乐"});
117. //System.out.String());
118. /**
119. * json对象转换成java对象
120. */
121. Student stud = (Student) Bean(jsonObj,Student.class);
122. System.out.Name());
123. }
124. }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论