java uses JSONObject instance
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();
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)
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()); }
For more articles related to using JSONObject instances in Java, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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...

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? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

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. ...

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 use OAuth2.0's access_token to achieve control of interface access permissions? In the application of OAuth2.0, how to ensure that the...

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

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