国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊(cè) 關(guān)于JSON以及JSON在PHP中的應(yīng)用技巧

關(guān)于JSON以及JSON在PHP中的應(yīng)用技巧

Jun 06, 2016 pm 08:26 PM
json about Application tips

這篇文章主要介紹了關(guān)于JSON以及JSON在PHP中的應(yīng)用技巧。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助

JSON 基礎(chǔ)

簡(jiǎn)單地說(shuō),JSON 可以將 JavaScript 對(duì)象中表示的一組數(shù)據(jù)轉(zhuǎn)換為字符串,然后就可以在函數(shù)之間輕松地傳遞這個(gè)字符串,或者在異步應(yīng)用程序中將字符串從 Web 客戶機(jī)傳遞給服務(wù)器端程序。這個(gè)字符串看起來(lái)有點(diǎn)兒古怪(稍后會(huì)看到幾個(gè)示例),但是 JavaScript 很容易解釋它,而且 JSON 可以表示比名稱/值對(duì)更復(fù)雜的結(jié)構(gòu)。例如,可以表示數(shù)組和復(fù)雜的對(duì)象,而不僅僅是鍵和值的簡(jiǎn)單列表。

簡(jiǎn)單 JSON 示例

按照最簡(jiǎn)單的形式,可以用下面這樣的 JSON 表示名稱/值對(duì):

{ "firstName": "Brett" }


這個(gè)示例非常基本,而且實(shí)際上比等效的純文本名稱/值對(duì)占用更多的空間:

firstName=Brett

但是,當(dāng)將多個(gè)名稱/值對(duì)串在一起時(shí),JSON 就會(huì)體現(xiàn)出它的價(jià)值了。首先,可以創(chuàng)建包含多個(gè)名稱/值對(duì)的記錄,比如:

{ "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" }


從語(yǔ)法方面來(lái)看,這與名稱/值對(duì)相比并沒有很大的優(yōu)勢(shì),但是在這種情況下 JSON 更容易使用,而且可讀性更好。例如,它明確地表示以上三個(gè)值都是同一記錄的一部分;花括號(hào)使這些值有了某種聯(lián)系。

值的數(shù)組

當(dāng) 需要表示一組值時(shí),JSON 不但能夠提高可讀性,而且可以減少?gòu)?fù)雜性。例如,假設(shè)您希望表示一個(gè)人名列表。在 XML 中,需要許多開始標(biāo)記和結(jié)束標(biāo)記;如果使用典型的名稱/值對(duì)(就像在本系列前面文章中看到的那種名稱/值對(duì)),那么必須建立一種專有的數(shù)據(jù)格式,或者將鍵 名稱修改為 person1-firstName 這樣的形式。

如果使用 JSON,就只需將多個(gè)帶花括號(hào)的記錄分組在一起:

復(fù)制代碼 代碼如下:


{ "people": [
?{ "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
?{ "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" },
?{ "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" }
]}


這不難理解。在這個(gè)示例中,只有一個(gè)名為 people 的變量,值是包含三個(gè)條目的數(shù)組,每個(gè)條目是一個(gè)人的記錄,其中包含名、姓和電子郵件地址。上面的示例演示如何用括號(hào)將記錄組合成一個(gè)值。當(dāng)然,可以使用相同的語(yǔ)法表示多個(gè)值(每個(gè)值包含多個(gè)記錄):

復(fù)制代碼 代碼如下:


{ "programmers": [
?{ "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
?{ "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" },
?{ "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" }
?],
"authors": [
?{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
?{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
?{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
?],
"musicians": [
?{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
?{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
?]
}


這里最值得注意的是,能夠表示多個(gè)值,每 個(gè)值進(jìn)而包含多個(gè)值。但是還應(yīng)該注意,在不同的主條目(programmers、authors 和 musicians)之間,記錄中實(shí)際的名稱/值對(duì)可以不一樣。JSON 是完全動(dòng)態(tài)的,允許在 JSON 結(jié)構(gòu)的中間改變表示數(shù)據(jù)的方式。

在處理 JSON 格式的數(shù)據(jù)時(shí),沒有需要遵守的預(yù)定義的約束。所以,在同樣的數(shù)據(jù)結(jié)構(gòu)中,可以改變表示數(shù)據(jù)的方式,甚至可以以不同方式表示同一事物。

在 JavaScript 中使用 JSON

掌握了 JSON 格式之后,在 JavaScript 中使用它就很簡(jiǎn)單了。JSON 是 JavaScript 原生格式,這意味著在 JavaScript 中處理 JSON 數(shù)據(jù)不需要任何特殊的 API 或工具包。

將 JSON 數(shù)據(jù)賦值給變量

例如,可以創(chuàng)建一個(gè)新的 JavaScript 變量,然后將 JSON 格式的數(shù)據(jù)字符串直接賦值給它:

復(fù)制代碼 代碼如下:


var people =
?{ "programmers": [
? { "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
? { "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" },
? { "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" }
? ],
?"authors": [
? { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
? { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
? { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
? ],
?"musicians": [
? { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
? { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
? ]
?}


這非常簡(jiǎn)單;現(xiàn)在 people 包含前面看到的 JSON 格式的數(shù)據(jù)。但是,這還不夠,因?yàn)樵L問(wèn)數(shù)據(jù)的方式似乎還不明顯。

訪問(wèn)數(shù)據(jù)

盡 管看起來(lái)不明顯,但是上面的長(zhǎng)字符串實(shí)際上只是一個(gè)數(shù)組;將這個(gè)數(shù)組放進(jìn) JavaScript 變量之后,就可以很輕松地訪問(wèn)它。實(shí)際上,只需用點(diǎn)號(hào)表示法來(lái)表示數(shù)組元素。所以,要想訪問(wèn) programmers 列表的第一個(gè)條目的姓氏,只需在 JavaScript 中使用下面這樣的代碼:

people.programmers[0].lastName;

注意,數(shù)組索引是從零開始的。所以,這行代碼首先訪問(wèn) people 變量中的數(shù)據(jù);然后移動(dòng)到稱為 programmers 的條目,再移動(dòng)到第一個(gè)記錄([0]);最后,訪問(wèn) lastName 鍵的值。結(jié)果是字符串值 “McLaughlin”。

下面是使用同一變量的幾個(gè)示例。

復(fù)制代碼 代碼如下:


people.authors[1].genre? // Value is "fantasy"
people.musicians[3].lastName // Undefined. This refers to the fourth entry,
?and there isn't one
people.programmers.[2].firstName // Value is "Elliotte"


利用這樣的語(yǔ)法,可以處理任何 JSON 格式的數(shù)據(jù),而不需要使用任何額外的 JavaScript 工具包或 API。

修改 JSON 數(shù)據(jù)

正如可以用點(diǎn)號(hào)和括號(hào)訪問(wèn)數(shù)據(jù),也可以按照同樣的方式輕松地修改數(shù)據(jù):

people.musicians[1].lastName = "Rachmaninov";

在將字符串轉(zhuǎn)換為 JavaScript 對(duì)象之后,就可以像這樣修改變量中的數(shù)據(jù)。

轉(zhuǎn)換回字符串

當(dāng)然,如果不能輕松地將對(duì)象轉(zhuǎn)換回本文提到的文本格式,那么所有數(shù)據(jù)修改都沒有太大的價(jià)值。在 JavaScript 中這種轉(zhuǎn)換也很簡(jiǎn)單:

String newJSONtext = people.toJSONString();

這樣就行了!現(xiàn)在就獲得了一個(gè)可以在任何地方使用的文本字符串,例如,可以將它用作 Ajax 應(yīng)用程序中的請(qǐng)求字符串。

更重要的是,可以將任何 JavaScript 對(duì)象轉(zhuǎn)換為 JSON 文本。并非只能處理原來(lái)用 JSON 字符串賦值的變量。為了對(duì)名為 myObject 的對(duì)象進(jìn)行轉(zhuǎn)換,只需執(zhí)行相同形式的命令:

String myObjectInJSON = myObject.toJSONString();

這就是 JSON 與本系列討論的其他數(shù)據(jù)格式之間最大的差異。如果使用 JSON,只需調(diào)用一個(gè)簡(jiǎn)單的函數(shù),就可以獲得經(jīng)過(guò)格式化的數(shù)據(jù),可以直接使用了。對(duì)于其他數(shù)據(jù)格式,需要在原始數(shù)據(jù)和格式化數(shù)據(jù)之間進(jìn)行轉(zhuǎn)換。即使使用 Document Object Model 這樣的 API(提供了將自己的數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為文本的函數(shù)),也需要學(xué)習(xí)這個(gè) API 并使用 API 的對(duì)象,而不是使用原生的 JavaScript 對(duì)象和語(yǔ)法。

最終結(jié)論是,如果要處理大量 JavaScript 對(duì)象,那么 JSON 幾乎肯定是一個(gè)好選擇,這樣就可以輕松地將數(shù)據(jù)轉(zhuǎn)換為可以在請(qǐng)求中發(fā)送給服務(wù)器端程序的格式。

JSON在PHP中的應(yīng)用

互聯(lián)網(wǎng)的今天,AJAX已經(jīng)不是什么陌生的詞匯了。說(shuō)起AJAX,可能會(huì)立即想起因RSS而興起的XML。XML的解析,恐怕已經(jīng)不是什么難題了,特別是 PHP5,大量的XML解析器的涌現(xiàn),如最輕量級(jí)的SimpleXML。不過(guò)對(duì)于AJAX來(lái)說(shuō),XML的解析更傾向于前臺(tái)Javascript的支持度。 我想所有解析過(guò)XML的人,都會(huì)因樹和節(jié)點(diǎn)而頭大。不可否認(rèn),XML是很不錯(cuò)的數(shù)據(jù)存儲(chǔ)方式,但是其靈活恰恰造成了其解析的困難。當(dāng)然,這里所指的困難, 是相對(duì)于本文的主角--JSON而言。
JSON為何物?我就不重復(fù)概念了。通俗的說(shuō),它是一種數(shù)據(jù)的存儲(chǔ)格式,就像PHP序列化后的字符串一樣。它是一種數(shù)據(jù)描述。比如我們將一 個(gè)數(shù)組序列化后存放,就可以很容易的反序列化后應(yīng)用。JSON也是如此,只不過(guò)他搭建的是客戶端Javascript和服務(wù)端PHP的交互橋梁。我們用 PHP生成JSON后的字符串,然后把這個(gè)字符串傳給前臺(tái)Javascript,Javascirpt就可以很容易的將其反JSON然后應(yīng)用。說(shuō)通俗點(diǎn), 它真的很像數(shù)組。
言歸正傳,如何使用JSON。PHP5.2開始內(nèi)置了JSON的支持。當(dāng)然,如果低于這個(gè)版本的話,那么市面上有很多PHP版本的實(shí)現(xiàn),隨 便下一個(gè)用就OK啦?,F(xiàn)在主要是說(shuō)說(shuō)PHP內(nèi)置支持的JSON。很簡(jiǎn)單,兩個(gè)函數(shù):json_encode和json_decode(跟序列化很像啦)。 一個(gè)編碼,一個(gè)解碼。先看看編碼的使用:

復(fù)制代碼 代碼如下:

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

How to use PHP functions to process JSON data? How to use PHP functions to process JSON data? May 04, 2024 pm 03:21 PM

PHP provides the following functions to process JSON data: Parse JSON data: Use json_decode() to convert a JSON string into a PHP array. Create JSON data: Use json_encode() to convert a PHP array or object into a JSON string. Get specific values ??of JSON data: Use PHP array functions to access specific values, such as key-value pairs or array elements.

Quick tips for converting PHP arrays to JSON Quick tips for converting PHP arrays to JSON May 03, 2024 pm 06:33 PM

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

ColorOS15 interface exposed. About this machine, here is a big change ColorOS15 interface exposed. About this machine, here is a big change Aug 28, 2024 pm 03:31 PM

Recently, ColorOS15 took the lead in launching internal beta testing. Some netizens exposed the relevant interface. Let’s see how it goes. As you can see in the picture above, some netizens posted the “About This Machine” interface of OPPO Find X7 after upgrading ColorOS15Beta. In addition to the big change in the top pattern, The configuration information in the lower half has also changed from the previous two columns to a single column vertical distribution. Attached is the "About This Phone/Mobile Phone" interface in the latest versions of mobile phones from six brands: Huawei, Honor, Xiaomi, OPPO, vivo, and Meizu. You can tell me which layout you like better. Regarding ColorOS 15, previous news said In addition to supporting LivePhoto live photos on a wide scale, it will also "support AirDrop&

Conquer the pinnacle of Java JSON processing: parse and create complex data Conquer the pinnacle of Java JSON processing: parse and create complex data Mar 09, 2024 am 09:13 AM

Parsing JSON data Parsing JSON data is a critical step in processing complex data. In Java, we can use the following methods: Use the Gson library: Gson is a widely used jsON parsing library that provides a concise and efficient api, as shown below: Gsongson=newGson();JsonObjectjsonObject=gson.fromJson(jsonString ,JsonObject.class); Using the Jackson library: Jackson is another popular JSON processing library that supports rich functionality and conversion to other formats (such as XML), as shown below: ObjectMappe

Is There an RSS Alternative Based on JSON? Is There an RSS Alternative Based on JSON? Apr 10, 2025 am 09:31 AM

JSONFeed is a JSON-based RSS alternative that has its advantages simplicity and ease of use. 1) JSONFeed uses JSON format, which is easy to generate and parse. 2) It supports dynamic generation and is suitable for modern web development. 3) Using JSONFeed can improve content management efficiency and user experience.

See all articles