Home Java javaTutorial java uses JSONObject instance

java uses JSONObject instance

Jan 19, 2017 pm 02:59 PM
jsonobject

1. Introduction of jar package
Using JSONObject must reference JSON-lib.jar, and it also depends on other packages
common-lang.jar
common-beanuitls.jar
common-collections .jar
common-logging.jar
ezmorph.jar
2. Use of JSONObject objects
The JSON-lib package is a package for converting java objects, xml, and JSON to each other.
1. Convert Java objects into json strings

Person p1=new Person();
p1.setName("刘大江");
p1.setAge(26);
String jsonStr=JSONObject.fromObject(p1).toString();
Copy after login

2. Convert Java collections into json strings

        Person p1=new Person();p1.setName("A1");p1.setAge(26);
        Person p2=new Person();p2.setName("A2");p2.setAge(23);

        List personList=new ArrayList<Person>();
        personList.add(p1);
        personList.add(p2);

        Map personMap=new HashMap<String, Person>();
        personMap.put("p1", p1);
        personMap.put("p2", p2);

        //[{"age":26,"name":"A1"},{"age":23,"name":"A2"}]
        JSONArray.fromObject(personList).toString();
 JSONSerializer.toJSON(personList)

        //[{"p2":{"name":"A2","age":23},"p1":{"name":"A1","age":26}}]
        JSONArray.fromObject(personMap).toString();
 JSONSerializer.toJSON(personMap)
Copy after login

3. Convert json strings into dynamic Java objects

 String jsonStr="[{\"name\":\"A2\",\"age\":23},{\"name\":\"A1\",\"age\":26}]";
        JSONArray ja=JSONArray.fromObject(jsonStr);

        for(int i=0;i<ja.size();i++){
            JSONObject jo= ja.getJSONObject(i); //转换成JSONObject对象
            System.out.println(jo.get("name"));

            Person p=(Person)JSONObject.toBean(jo,Person.class);    //转换成JavaBean
            System.out.println(p.getName()); 

        }
Copy after login

For more articles related to using JSONObject instances in Java, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to convert names to numbers to implement sorting within groups? How to convert names to numbers to implement sorting within groups? Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Ultimate consistency in distributed systems: how to apply and how to compensate for data inconsistencies? Ultimate consistency in distributed systems: how to apply and how to compensate for data inconsistencies? Apr 19, 2025 pm 02:24 PM

Exploring the application of ultimate consistency in distributed systems Distributed transaction processing has always been a problem in distributed system architecture. To solve the problem...

How to restrict access to specific interfaces of nested H5 pages through OAuth2.0's scope mechanism? How to restrict access to specific interfaces of nested H5 pages through OAuth2.0's scope mechanism? Apr 19, 2025 pm 02:30 PM

How to use OAuth2.0's access_token to achieve control of interface access permissions? In the application of OAuth2.0, how to ensure that the...

How to analyze the cracking process of IntelliJ IDEA and find the lib or class responsible for registration? How to analyze the cracking process of IntelliJ IDEA and find the lib or class responsible for registration? Apr 19, 2025 pm 04:00 PM

Regarding the analysis method of IntelliJIDEA cracking in the programming world, IntelliJ...

How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? Apr 19, 2025 pm 07:15 PM

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

See all articles