search
Homephp教程PHP开发php json function usage
php json function usageNov 29, 2016 pm 03:58 PM

Today I submitted a JSON data to PHP using POST method (pay attention to GET method too), for example:

{"a":1,"b":2}

Retrieve this data in PHP: $s=$ _POST['data'] ;//or $_GET['data'], then this string is escaped after being taken out: {"a":1,"b":2}

If called directly:

$obj = json_decode($s);

print_r($obj);

echo $obj->a;

is wrong and an error will be reported. If $s is directly defined: $s='{"a ":1,"b":2}'; then there is no problem. Therefore, when processing JSON in PHP, you need to perform some escaping processing: $s=strips tutorial lashes($_POST['data']); Then proceed to json Just decode it.

json_decode — Encode JSON formatted strings

json_encode — JSON encode variables

Report a bug parameter

value

The value to be encoded can be any data type except resource type. This function can only accept UTF-8 encoded data (Translation: refers to character/string type data)

Report a bug Return value

If the encoding is successful, a string represented in JSON form will be returned.

Report a bug Example

Example #1 A json_encode() example, the code is as follows:

$arr = array ('a'=>1,'b'=>2, 'c'=>3,'d'=>4,'e'=>5);

echo json_encode($arr);

?>

//The above routine will output :

{"a":1,"b":2,"c":3,"d":4,"e":5}

json_encode — JSON encoding of variables

json_decode — JSON format The string is encoded

Report a bug Description

mixed json_decode ( string $json [, bool $assoc ] )

Accepts a JSON format string and converts it into a PHP variable

Report a bug Parameters

json

A string in json string format to be decoded.

assoc

When this parameter is TRUE, array will be returned instead of object.

Report a bug Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.

Report a bug Example

Example #1 Example of json_decode()

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json) ; a"] => int(1)

["b"] => int(2)

["c"] => int(3)

["d"] => int( 4)

["e"] => int(5)

}

array(5) {

["a"] => int(1)

["b"] = > int(2)

["c"] => int(3)

["d"] => int(4)

["e"] => int(5)

}

*/

Statement
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.