Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Json

Shulou Source: shulou.com Published: 2022-06-02 13:42:26 09月22日 Update

This article mainly explains "how to use Json". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Json.

Json is generally used in a small amount of data processing. Because the format is simple and easy to operate, and javascript can support the processing function of Json format. Therefore, it is recommended that you use

The general format of Json is as follows: {"id": "1", "name": "abc"} or [{"id": "1", "name": "abc"}, {"id": "1", "name": "abc"}]

Here are several ways to interpret the Json format in js:

Eval ('('+ Json+')')

Why do you write this: mainly because in JavaScript, expression statements are not allowed to start with the left curly bracket "{", which will be confused with block statements. To solve this problem, you need to add parentheses when parsing JSON text using eval (). Parentheses are used as grouping operators to apply for jobs for expressions surrounded by parentheses.

Var strJSON= (new Function ("return" + Json)) ()

Interpret Json by creating methods

Var strJSON= JSON.parse (Json)

Note: the Json format that can be interpreted by this method must be enclosed in double quotation marks for key-value pairs, otherwise the Json format cannot be explained.

Example

Json.aspx page

/ / Asynchronous processing of Json objects $("# btn") .click (function () {$.ajax ({url: "Json.ashx", type: "post", data: {id: "123"}, datatype:" Json ", success: function (data) {var strData = data; alert (" 1-"); var str1 = eval (" ("+ data +") ") / / * js interpretation Json alert ("2 -"); var str2 = (new Function ("return" + strData)) (); / / second js interpretation Json alert (str2.id + "@" + str2.name); alert ("3 -"); var str3 = JSON.parse (strData); alert (str3.id + "@" + str3.name) / / processing multidimensional Json var strSs = ""; alert (str1.length); for (var I = 0; I)

Tags: Format interpretation processing method content parenthesis expression statement multidimensional learning practical deeper interest function example practicality actual object suggestion quotation mark Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB Docker vpn MySQL