Detailed explanation of JSONObject usage

Detailed explanation of JSONObject usage

JSONObject is just a data structure, which can be understood as a data structure in JSON format ( key-value structure). You can use the put method to add elements to the JSON object. JSONObject can be easily converted into a string, and other objects can also be easily converted into JSONObject objects.

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 structure

     HashMap<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 entities

        Student 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 object

String 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 listJson

ArrayList<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:
  • Java JSONObject and JSONArray object case study
  • Conversion between JSONObject objects and Map objects in JAVA
  • Detailed explanation of String JSONObject JSONArray List<entity class> conversion in Java
  • Analysis of JSONObject and JSONArray usage
  • Java uses JSONObject to operate json instance parsing
  • Detailed explanation of the difference between JSONObject and JSONArray in Java
  • Methods for converting JSON string to JSONObject and JSONArray
  • Use of JSONObject and JSONArray
  • Detailed explanation of how to use JSONObject

<<:  Tutorial on installing mysql5.7.36 database in Linux environment

>>:  Use html-webpack-plugin' to generate HTML page plugin in memory

Recommend

Negative distance (empathy) - iterative process of mutual influence

Negative distance refers to empathy. Preface (rai...

JavaScript design pattern chain of responsibility pattern

Table of contents Overview Code Implementation Pa...

Example of how to configure nginx in centos server

Download the secure terminal MobaXterm_Personal F...

Detailed explanation of common methods of Vue development

Table of contents $nextTick() $forceUpdate() $set...

A mobile adaptive web page effect solves the problem of small display page

For work needs, I need to make a mobile phone adap...

How to export and import .sql files under Linux command

This article describes how to export and import ....

A Deep Dive into JavaScript Promises

Table of contents 1. What is Promise? 2. Why is t...

10 Website Usability Tips Everyone Should Know

Let’s not waste any more time and get straight to...

Implementation of nginx worker process loop

After the worker process is started, it will firs...

In-depth explanation of MySQL learning engine, explain and permissions

engine Introduction Innodb engine The Innodb engi...

The difference between div and table in speed, loading, web application, etc.

1: Differences in speed and loading methods The di...

$nextTick explanation that you can understand at a glance

Table of contents 1. Functional description 2. Pa...

Add a copy code button code to the website code block pre tag

Referring to other more professional blog systems...

jQuery realizes the shuttle box effect

This article example shares the specific code of ...