Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of json in java

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about the use of json in java. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Josn:

A lightweight data transfer format that is independent of the development language

At first, it was javaScript, but later it became popular. Almost all languages have corresponding API data structures:

Object--- object

Use the key-value pair structure contained in curly braces {}

Key must be of type String, and value must be any basic type or data structure (object, or array)

Array--- array

Start with brackets [] and separate elements with commas

Basic types:

String number true false null

For example:

{"name": "Wang Xiaoer", "age": 22, "birthday": "1990-12-1". Note: there is no Date time type in json, so use string. You need to know about the timestamp "school": "Lanxiang", "major": ["haircut", "excavator"], "has_girlfriend": false, "car": null, "house": null, "commont": "this is a comment"} Note: there is no comment in json, but we can save the nation by curve {"id": "1". "author": {"id": "1506200043", "name": "kylin", "gender": 1}, "major": ["haircut", "excavator"]}

Java supports json. The following packages need to be introduced when using:

Commons-beanutils.xxx.jar commons-collections.xxx.jar commons-lang.xxx.jar commons-logging.jar json-lib.xxx.jar ezmorph.xxx.jar

Common ways to generate JSON strings:-data sent from the back end to the front end

Use map to put key-value data, then convert it to a json object-- then convert it to a json string-- and send it to the front end

/ / map (javaObject)-> jsonObject---- > jsonStr-Front end import net.sf.json.JSONObject;public void jsonStrByMap () {/ / build map objects and add into Mapparams=new HashMap () what needs to be sent to the front end into Mapparams=new HashMap (); params.put ("status", "200"); params.put ("username", "kylin"); params.put ("password", "123456"); params.put ("sexy", "male"); params.put ("userID", "15062") / / java object becomes json object JSONObject jsonObject=JSONObject.fromObject (params); / / json object is converted into json string String jsonStr=jsonObject.toString (); System.out.println (jsonStr);}

Use javabean to build json

User.javapublic class User {private String username; private String password;public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password;}} / / javabean---json object-json string-> frontend public void jsonStrByJavaBean () {/ / create a User object User user=new User (); user.setUsername ("kylin") User.setPassword ("123456"); / / java object converted to json object JSONObject jsonObject=JSONObject.fromObject (user); / / json object converted to json string String jsonStr=jsonObject.toString (); System.out.println (jsonStr);}

Data obtained from the front end-> converted into data that can be used at the back end

Convert json strings to java objects

The / / front end passes the json string, so we need to get the string and process / / json string-json object-java object JSONObject jsonobject = JSONObject.fromObject (jsonStr); User user= (User) JSONObject.toBean (jsonobject,User.class); / / then manipulate String username=user.getUsername (); String password=user.getPassword () through the user object

Convert json strings to map objects

JSONObject jsonobject = JSONObject.fromObject (jsonStr); Map params= (Map) jsonObject;// can then operate through map to determine whether there are username fields and username fields if (params.containsKey ("username")) {System.out.println (params.get ("username"));} if (params.containsKey ("password")) {System.out.println (params.get ("password"));}

Summary:

Json is a front-end and back-end communication specification, which is described above as JSON (official library in android SDK). It is generally used in mobile clients, and GSON (an open source library of Google) will be introduced later. It is generally used in server-side backend development, and its function is relatively powerful.

Summary of the JSON library:

Function: mapping java objects and json strings

Declare through Annotation annotations

Custom attribute names are supported

Support for including or excluding attributes

Support custom excuses to complete the parsing / generation process by yourself

Process:

The backend uses java to write json

The front end uses javascript to parse json strings directly.

The android client uses the json class to resolve

This is what the editor shares with you about the use of json in java. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report