Several ways to generate JSON

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[several ways to generate JSON]

Several ways of generating JSON in [small java class of Xiuzhen academy]

Hello, I'm wang Zhixiong, the 17th student of Wuhan Branch of it Academy. I'm an honest, pure and kind java programmer. Today, I'd like to share with you the Java task 2 on the official website of the Academy, the knowledge points in deep thinking - several ways to generate JSON

(1) Background:

JSON, the full name is JavaScript object notation, that is, JavaScript object notation. Is a lightweight data exchange format. Easy to read and write. It is also easy to machine parse and generate. Therefore, using JSON to transfer data can achieve simple and fast results.

(2) Knowledge analysis:

How to generate JSON?

The generated JSON string is used for front-end and back-end interaction. When the current end requests with Ajax, the back end returns a JSON string.

There are three ways to generate JSON:

Apply JSON taglib and set fields or nesting in JSP pages.

With the same annotation, ResponseBody returns Java objects or collections, which can be automatically converted into JSON strings.

Using gson transformation.

Why is JSON taglib recommended to handle JSON

Gson, which provides the tojason () method to convert an object into a JSON string

Gson can also be deserialized and provides the fromjson () method to implement the method from JSON related objects to Java entities.

When using @ ResponseBody annotation,

If the annotation is marked on the controller method, the object returned by the method will not be rendered by the view parser, but will be converted into the specified format according to the content of the accept field of the HTTP request header and written into the body data area of the response object. If there is no additional configuration, the data in JSON format will be returned

So the benefits of JSON taglib are reflected:

1. Using JSON taglib, the code in the controller is more concise and easy to read

2. Using JSON taglib is more flexible. If you need to change the JSON data format in the future, you only need to change the JSP page without changing the controller code

(3) Frequently asked questions:

Jason application

Read JSON string

Handle JSON strings with eval()

JSON can be passed as an array or an object. The returned JSON format is an array JSON, which contains objects, which is a common format of JSON. Later, after PHP processing, the structure changed again. The outermost layer is an object, in which are arrays and objects.

In the data transmission process, JSON is transmitted in the form of text, that is, string, and JS operates on JSON objects. Therefore, the mutual conversion between JSON objects and JSON strings is the key. For example:

JSON string:

var str1 = '{ "name": "haorooms","sex": "man" }';

JSON object:

var str2 = { "name": "haorooms","sex": "man" };

When RPC is called remotely or provided to an external access interface, the general data interaction format is usually JSON

Provide mobile app interface

(4) Coding practice:

(5) Expand thinking:

The difference between JSON and XML

Advantages of XML

A. The format is unified and meets the standards;

B. It is easy to interact with other systems remotely, and data sharing is more convenient.

Disadvantages of XML

A. XML file is huge, the file format is complex, and the transmission takes up bandwidth;

B. Both server-side and client-side need to spend a lot of code to parse XML, which makes the server-side and client-side code extremely complex and difficult to maintain;

C. The way of parsing XML between different browsers on the client is inconsistent, and a lot of code needs to be written repeatedly;

D. Parsing XML on the server side and client side takes more resources and time.

Advantages and disadvantages of JSON

A. The data format is relatively simple and easy to read and write. The formats are compressed and occupy less bandwidth

B. It is easy to parse, and the client JavaScript can simply read JSON data through eval();

C. Support multiple languages, including ActionScript, C, c#, ColdFusion, Java, JavaScript, Perl, PHP, python, ruby and other server-side languages to facilitate server-side parsing;

D. In the PHP world, php-json and json-php have appeared. It is inclined to call the program directly after PHP serialization. The objects and arrays on the PHP server can directly generate JSON format, which is convenient for the client to access and extract;

E. Because JSON format can be directly used by server-side code, it greatly simplifies the amount of code development on server-side and client-side, and the completion task remains unchanged, and it is easy to maintain.

Disadvantages of JSON

A. It is not as popular and widely used as XML format, and it is not as universal as XML. At present, the promotion of JSON format in Web service is still in its infancy.

(6) References:

https://blog.csdn.net/android_zhengyongbo/article/details/67634565

(7) More discussion:

Q1: what are the methods of JSON and XML parsing

A1: JSON parsing method (Alibaba fastjson, Google gson, jackjason) XML parsing method (DOM, sax, pul)

Q2: why is the efficiency of using annotation reponse higher than JSON taglib?

A2: because the annotation method is used, the JSON object is returned directly without view rendering. The final return of JSON taglib is the JSP interface, which needs to be parsed by the view parser.

Q3: how to make a JSON call using jQuery?

A3: for example, in an MVC, the JSON object of employee can be returned through the "getemployee" function.

This PPT is completed on the basis of Wang Tianchi. Thank you!

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>