JSON is based on a subset of JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON uses a completely language-independent text format, but also uses conventions similar to the C language family (including C, C++, C#, Java, JavaScript, Perl, Python, etc.). These properties make JSON an ideal data exchange language.
JSON is constructed from two structures:
A collection of name/value pairs. In different languages, it is understood as an object, a record, a struct, a dictionary, a hash table, a keyed list, or an associative array. array).
An ordered list of values. In most languages, it is understood as an array.
PHP’s serialize is to serialize variables and return a string expression with variable type and structure.
Speaking of which, both embody a data structure in the form of a string. , so what is the difference between them.
Let’s start with JSON and look at a simple example.
Example 1:
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">var</font></strong></span><span> test = {</span><span class="string"><font color="#0000ff">"Name"</font></span><span>:</span><span class="string"><font color="#0000ff">"Peter"</font></span><span>,</span><span class="string"><font color="#0000ff">"Age"</font></span><span>:20}; </span></span></li> <li> <span>document.write(test.Name + </span><span class="string"><font color="#0000ff">": "</font></span><span> + test.Age); </span> </li> </ol>
Display results:
Peter: 20
{"Name":"Peter","Age in variable test ":20} is an object with 2 elements (it feels like an array in PHP):
Name is Peter and Age is 20.
Of course it can get more complicated.
Example 2:
<ol class="dp-xml"> <li class="alt"><span><span>var </span><span class="attribute"><font color="#ff0000">test</font></span><span> = {"User":{"Name":"Peter","Age":20},"Company":"FORD"}; </span></span></li> <li><span>document.write(test.User.Name + ": " + test.Company); </span></li> </ol>
Display results:
Peter: FORD In this example, the User element contains Name and Age.
If you want to reflect multiple Users, you need to use an array. Different from the "{}" of the object, the array uses "[]".
JSON parsing example three:
<ol class="dp-xml"> <li class="alt"><span><span>var </span><span class="attribute"><font color="#ff0000">test</font></span><span> = [ </span></span></li> <li><span> {"User":{"Name":"Peter","Age":20},"Company":"FORD"}, </span></li> <li class="alt"><span> {"User":{"Name":"Li Ming","Age":20},"Company":"Benz"} </span></li> <li><span> ]; </span></li> <li class="alt"><span>document.write(test[1].User.Name + ": " + test[1].Company); </span></li> <li><span>//或者使用:document.write(test[1]["User"]["Name"] + ": " + test[1]["Company"]); </span></li> </ol>
JSON parsing display results:
Li Ming: Benz
Through the above simple examples, some complex The data is passed through a string, and it is indeed much more convenient to use Ajax.
Let’s take a look at the role of PHP’s serialize function.
JSON parsing example four:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">arr</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span> </span></span></li> <li><span> ( </span></li> <li class="alt"> <span> 'Peter'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> array </span> </li> <li><span> ( </span></li> <li class="alt"> <span> 'Country'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'USA', </span> </li> <li> <span> 'Age'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>20 </span> </li> <li class="alt"><span> ), </span></li> <li> <span> 'Li Ming'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> array </span> </li> <li class="alt"><span> ( </span></li> <li> <span> 'Country'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'CHINA', </span> </li> <li class="alt"> <span> 'Age'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>21 </span> </li> <li><span> ) </span></li> <li class="alt"><span> ); </span></li> <li><span> </span></li> <li class="alt"> <span>$</span><span class="attribute-value"><font color="#0000ff">serialize</font></span><span class="attribute"><font color="#ff0000">serialize_var</font></span><span> = serialize($arr); </span> </li> <li><span>echo $serialize_var; </span></li> </ol>
JSON parsing display result:
<ol class="dp-xml"><li class="alt"><span><span>a:2:{s:5:"Peter";a:2:{s:7:"Country";s:3:"USA";s:3:"Age";i:20;}s:7:"Li Ming";a:2:{s:7:"Country";s:5:"CHINA";s:3:"Age";i:21;}} </span></span></li></ol>
This result looks more complicated than JSON, but it is actually very simple. What is explained is some data types and structures.
Take a:2:{s:7:"Country";s:3:"USA";s:3:"Age";i:20;} as an example:
a:2 Indicates that this is an array with two elements. s:7:"Country";s:3:"USA"; is the first element. s:7 indicates that this is a string with 7 characters. ), followed by i:20; should also be guessed to be an integer (integer) 20.
Look at this example again,
Example 5:
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">class</font></strong></span><span> test </span></span></li> <li><span>{ </span></li> <li class="alt"> <span> </span><span class="keyword"><strong><font color="#006699">var</font></strong></span><span> </span><span class="vars"><font color="#dd0000">$var</font></span><span> = 0; </span> </li> <li> <span> </span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> add(){ </span> </li> <li class="alt"> <span> </span><span class="func">echo</span><span> </span><span class="vars"><font color="#dd0000">$var</font></span><span>+10; </span> </li> <li><span> } </span></li> <li class="alt"><span>} </span></li> <li><span> </span></li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$unserialize_var</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> test; </span> </li> <li> <span></span><span class="vars"><font color="#dd0000">$serialize_var</font></span><span> = serialize(</span><span class="vars"><font color="#dd0000">$unserialize_var</font></span><span>); </span> </li> <li class="alt"> <span></span><span class="func">echo</span><span> </span><span class="vars"><font color="#dd0000">$serialize_var</font></span><span>; </span> </li> <li> <span></span><span class="vars"><font color="#dd0000">$unserialize_var</font></span><span> = null; </span> </li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$unserialize_var</font></span><span> = unserialize(</span><span class="vars"><font color="#dd0000">$serialize_var</font></span><span>); </span> </li> <li> <span></span><span class="vars"><font color="#dd0000">$unserialize_var</font></span><span>->add(); </span> </li> </ol>
Display results:
O:4:"test":1:{ s:3:"var";i:0;}
10
As you can see from this example, serialize saves both the type and structure of the data, and
unserialize The variables after can still use the add() method.
So is there any connection between PHP and JSON? Friends who are familiar with PHP should know that PHP5.2.0 has set JSON extension as the default component, which means that we can perform JSON operations in PHP, and its functions are json_encode and json_decode.
Example 6:
<ol class="dp-c"> <li class="alt"><span><span class="vars"><font color="#dd0000">$arr</font></span><span> = </span><span class="keyword"><strong><font color="#006699">array</font></strong></span><span> </span></span></li> <li><span> ( </span></li> <li class="alt"> <span> </span><span class="string"><font color="#0000ff">'Name'</font></span><span>=></span><span class="string"><font color="#0000ff">'Peter'</font></span><span>, </span> </li> <li> <span> </span><span class="string"><font color="#0000ff">'Age'</font></span><span>=>20 </span> </li> <li class="alt"><span> ); </span></li> <li><span> </span></li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$jsonencode</font></span><span> = json_encode(</span><span class="vars"><font color="#dd0000">$arr</font></span><span>); </span> </li> <li> <span></span><span class="func">echo</span><span> </span><span class="vars"><font color="#dd0000">$jsonencode</font></span><span>; </span> </li> </ol>
Display results:
{"Name":"Peter","Age":20}
This result is the same as the test value in Example 1. Use json_encode to convert the variables in PHP into JSON characters and output the expression.
Let’s take a look at the usage of json_decode.
Example 7:
<ol class="dp-c"> <li class="alt"><span><span class="vars"><font color="#dd0000">$var</font></span><span> = </span><span class="string"><font color="#0000ff">'{"Name":"Peter","Age":20}'</font></span><span>; </span></span></li> <li> <span></span><span class="vars"><font color="#dd0000">$jsondecode</font></span><span> = json_decode(</span><span class="vars"><font color="#dd0000">$var</font></span><span>); </span> </li> <li class="alt"> <span>print_r(</span><span class="vars"><font color="#dd0000">$jsondecode</font></span><span>); </span> </li> </ol>
Display results:
<ol class="dp-c"><li class="alt"><span><span>stdClass Object ( [Name] => Peter [Age] => 20 ) </span></span></li></ol>
This is indeed verified, in JSON {"Name":"Peter","Age" :20} is an object, but it can also be converted into an array in PHP by setting the ASSOC parameter to True in json_decode.
Example 8:
<ol class="dp-c"> <li class="alt"><span><span class="vars"><font color="#dd0000">$var</font></span><span> = </span><span class="string"><font color="#0000ff">'{"Name":"Peter","Age":20}'</font></span><span>; </span></span></li> <li> <span></span><span class="vars"><font color="#dd0000">$jsondecode</font></span><span> = json_decode(</span><span class="vars"><font color="#dd0000">$var</font></span><span>,true); </span> </li> <li class="alt"> <span>print_r(</span><span class="vars"><font color="#dd0000">$jsondecode</font></span><span>); </span> </li> </ol>
Display results:
<ol class="dp-c"><li class="alt"><span><span>Array ( [Name] => Peter [Age] => 20 ) </span></span></li></ol>
In addition, it should be noted that JSON is based on Unicode format, so to perform Chinese operations, it must be converted into UTF -8 format. Through the above examples, I believe everyone has a preliminary understanding of serialize and json_encode of JSON and PHP. By combining PHP, Javascript, JSON and Ajax, powerful data interaction functions can be completed.

Python中的支持向量机(SupportVectorMachine,SVM)是一个强大的有监督学习算法,可以用来解决分类和回归问题。SVM在处理高维度数据和非线性问题的时候表现出色,被广泛地应用于数据挖掘、图像分类、文本分类、生物信息学等领域。在本文中,我们将介绍在Python中使用SVM进行分类的实例。我们将使用scikit-learn库中的SVM模

随着新一代前端框架的不断涌现,VUE3作为一个快速、灵活、易上手的前端框架备受热爱。接下来,我们就来一起学习VUE3的基础知识,制作一个简单的视频播放器。一、安装VUE3首先,我们需要在本地安装VUE3。打开命令行工具,执行以下命令:npminstallvue@next接着,新建一个HTML文件,引入VUE3:<!doctypehtml>

Golang是一门功能强大且高效的编程语言,可以用于开发各种应用程序和服务。在Golang中,指针是一种非常重要的概念,它可以帮助我们更灵活和高效地操作数据。指针转换是指在不同类型之间进行指针操作的过程,本文将通过具体的实例来学习Golang中指针转换的最佳实践。1.基本概念在Golang中,每个变量都有一个地址,地址就是变量在内存中的位置。

VAE是一种生成模型,全称是VariationalAutoencoder,中文译作变分自编码器。它是一种无监督的学习算法,可以用来生成新的数据,比如图像、音频、文本等。与普通的自编码器相比,VAE更加灵活和强大,能够生成更加复杂和真实的数据。Python是目前使用最广泛的编程语言之一,也是深度学习的主要工具之一。在Python中,有许多优秀的机器学习和深度

随着互联网的普及,验证码已经成为了登录、注册、找回密码等操作的必要流程。在Gin框架中,实现验证码功能也变得异常简单。本文将介绍如何在Gin框架中使用第三方库实现验证码功能,并提供示例代码供读者参考。一、安装依赖库在使用验证码之前,我们需要安装一个第三方库goCaptcha。安装goCaptcha可以使用goget命令:$goget-ugithub

随着互联网的迅速发展,数据已成为了当今信息时代最为重要的资源之一。而网络爬虫作为一种自动化获取和处理网络数据的技术,正越来越受到人们的关注和应用。本文将介绍如何使用PHP开发一个简单的网络爬虫,并实现自动化获取网络数据的功能。一、网络爬虫概述网络爬虫是一种自动化获取和处理网络资源的技术,其主要工作过程是模拟浏览器行为,自动访问指定的URL地址并提取所

生成对抗网络(GAN,GenerativeAdversarialNetworks)是一种深度学习算法,它通过两个神经网络互相竞争的方式来生成新的数据。GAN被广泛用于图像、音频、文字等领域的生成任务。在本文中,我们将使用Python编写一个GAN算法实例,用于生成手写数字图像。数据集准备我们将使用MNIST数据集作为我们的训练数据集。MNIST数据集包含

快速上手Django框架:详细教程和实例引言:Django是一款高效灵活的PythonWeb开发框架,由MTV(Model-Template-View)架构驱动。它拥有简单明了的语法和强大的功能,能够帮助开发者快速构建可靠且易于维护的Web应用程序。本文将详细介绍Django的使用方法,并提供具体实例和代码示例,帮助读者快速上手Django框架。一、安装D


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
