JSONObject is just a data structure, which can be understood as a data structure in JSON format ( pom: <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency> 1. Generate json data format natively.JSONObject zhangsan = new JSONObject(); try { //Add zhangsan.put("name", "张三"); zhangsan.put("age", 18.4); zhangsan.put("birthday", "1900-20-03"); zhangsan.put("majar", new String[] {"哈哈","嘿嘿"}); zhangsan.put("null", null); zhangsan.put("house", false); System.out.println(zhangsan.toString()); } catch (JSONException e) { e.printStackTrace(); } 2. Generated through hashMap data structureHashMap<String, Object> zhangsan = new HashMap<>(); zhangsan.put("name", "张三"); zhangsan.put("age", 18.4); zhangsan.put("birthday", "1900-20-03"); zhangsan.put("majar", new String[] {"哈哈","嘿嘿"}); zhangsan.put("null", null); zhangsan.put("house", false); System.out.println(new JSONObject(zhangsan).toString()); 3. Generate through entitiesStudent student = new Student(); student.setId(1); student.setAge("20"); student.setName("张三"); //Generate json format System.out.println(JSON.toJSON(student)); //Convert the object to string String stuString = JSONObject.toJSONString(student); 4.Convert JSON string into JSON objectString studentString = "{\"id\":1,\"age\":2,\"name\":\"zhang\"}"; //Convert JSON string into JSON object JSONObject jsonObject1 = JSONObject.parseObject(stuString); System.out.println(jsonObject1); 5. Convert list object to listJsonArrayList<Student> studentLsit = new ArrayList<>(); Student student1 = new Student(); student1.setId(1); student1.setAge("20"); student1.setName("asdasdasd"); studentLsit.add(student1); Student student2 = new Student(); student2.setId(2); student2.setAge("20"); student2.setName("aaaa:;aaa"); studentLsit.add(student2); // list to json string String string = JSON.toJSON(studentLsit).toString(); System.out.println(string); //Convert json string to listJson format JSONArray jsonArray = JSONObject.parseArray(string); System.out.println(jsonArray); Alibaba's json is very easy to use, and Google's Gson is also good. If you are interested, you can take a look This concludes this article on the detailed usage of JSONObject. I hope it will be helpful for everyone’s study, and I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Tutorial on installing mysql5.7.36 database in Linux environment
>>: Use html-webpack-plugin' to generate HTML page plugin in memory
What is "Sticky Footer" The so-called &...
Today I will talk about a CSS special effect of h...
Error description When we install Docker Desktop,...
1. WebDesignerWall 2. Veerle's Blog 3. Tutori...
Table of contents 1. Parameters that determine ca...
Table of contents 1. The direction of this in the...
Preface Vue (pronounced /vjuː/, similar to view) ...
Table of contents Preface Problem: Large file cop...
The program is executed sequentially from top to ...
The following problem occurred when installing my...
Similar structures: Copy code The code is as foll...
1.0 Redis persistence Redis is an in-memory datab...
Table of contents front end According to the abov...
Web Services are concerned with application-to-ap...
MySQL reports an error when executing multi-table...