In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge about the method of encapsulating data in json. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Case introduction
Do you still remember the JSON returned by the 100 api interface of Express Express? When we ask for this link:
Link: http://www.kuaidi100.com/query?type=ems&postid=1111111111111
The following results are returned:
{"message": "ok", "nu": "1111111111111", "ischeck": "1", "com": "ems", "status": "200"," condition ":" F00 "," state ":" 3 "," data ": [{" time ":" 2021-05-26 09:45:08 " "context": "No results", "ftime": "2021-05-26 09:45:08"}]}
After looking at other api documents, we found that many of the json returned by api have certain characteristics. For example, all have status, all have message and so on. Next, the editor will take you to understand what data a json should have, right?
Analysis.
What data a json should have should be discussed by the front-end and back-end. The editor lists some options for your reference:
Status: response status, a json should tell the front end the return status to facilitate front-end processing. For example, when 404 occurs, it should jump to the 404 page through the front end. This is where the response information is used.
Message: response message. This information is generally used to report an error. If it is correct, the ok is returned. If there is an error, the backend will enter the error into this field, such as an auxiliary prompt such as insufficient permissions. The frontend can also display this information to the user.
Data: used to store data, which is usually stored in an array. Data stored in an object array is usually used to store list data.
Page: the number of pages in which paging is stored. In the case of data paging, a page value is generally returned. The front end can determine the number of pages currently paged for this page value.
Some json will also define some return values according to the requirements of the front end, such as title, url, time, etc., and the customization of json data is obtained after communication between the front and back ends.
How to encapsulate JSON data at the back end
The following is the json encapsulation code of a web project developed by Xiaobian, which can be used as a reference:
Set the status code:
Public enum ResultType {SUCCESS, Operation failed, UNAUTHORIZED, insufficient permissions, NOT_FOUND, INTERNAL_SERVER_ERROR, Server Internal error, AGAIN_LOGIN; private String code; private String name ResultType (String code, String name) {this.code = code; this.name = name;} public String getCode () {return code;} public String getName () {return name;}}
Json encapsulation:
Import java.io.Serializable;// request returns result object public class Result implements Serializable {/ / return status code private String statusCode = ResultType.SUCCESS.getCode (); / / return prompt message private String message = ResultType.SUCCESS.getName (); / / return result private T data = null; / / whether private Boolean success = true / / get status code public String getStatusCode () {return statusCode;} / / set status code @ param statusCode public void setStatusCode (String statusCode) {this.statusCode = statusCode;} / / get prompt information public String getMessage () {return message } / / set prompt information @ param message public void setMessage (String message) {this.message = message;} / / get the returned result @ return public T getData () {return data } / / return result @ param data public void setData (T data) {this.data = data;} public Boolean isSuccess () {return success;} public void setSuccess (Boolean success) {this.success = success } public void addError () {this.addError (");} public void addError (String message) {this.success = false; this.message = message; this.statusCode = ResultType.INTERNAL_SERVER_ERROR.getCode () If (this.message = = null | | ".equals (this.message)) {this.message = ResultType.INTERNAL_SERVER_ERROR.getName ();}} public void success () {this.success (");} public void success (String message) {this.success = true This.message = message; this.statusCode = ResultType.SUCCESS.getCode (); if (this.message = = null | | ".equals (this.message)) {this.message = ResultType.SUCCESS.getName ();} public void fail () {this.fail (") } public void fail (String message) {this.success = false; this.message = message; this.statusCode = ResultType.FAIL.getCode (); if (this.message = = null | | ".equals (this.message)) {this.message = ResultType.FAIL.getName () }} public void unauthorized () {this.unauthorized (");} public void unauthorized (String message) {this.success = false; this.message = message; this.statusCode = ResultType.UNAUTHORIZED.getCode () If (this.message = = null | | ".equals (this.message)) {this.message = ResultType.UNAUTHORIZED.getName ();}} public void notFound () {this.notFound (");} public void notFound (String message) {this.success = false This.message = message; this.statusCode = ResultType.NOT_FOUND.getCode (); if (this.message = = null | | ".equals (this.message)) {this.message = ResultType.NOT_FOUND.getName ();} public void againLogin () {this.againLogin (") } public void againLogin (String message) {this.success = false; this.message = message; this.statusCode = ResultType.AGAIN_LOGIN.getCode (); if (this.message = = null | | ".equals (this.message)) {this.message = ResultType.AGAIN_LOGIN.getName () } this is all the content of the article "what is the method of encapsulating data in json". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.