Json_decode returning an array or object in PHP _php tutorial

Source: Internet
Author: User
Tags php language
1.json_decode ()

Json_decode

(PHP 5 >= 5.2.0, PECL json >= 1.2.0)

json_decode-encoding a JSON-formatted string

Description

Mixed Json_decode (String $json [, bool $assoc])

Accept a JSON-formatted string and convert it to a PHP variable

Parameters

Json

String in JSON string format to decode.

Assoc

When this argument is TRUE, an array is returned instead of an object.

return value

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

Example

Examples of Example #1 Json_decode ()

The code is as follows

$json = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} ';
Var_dump (Json_decode ($json));
Var_dump (Json_decode ($json, true));
?>

The example above will output:

Object (StdClass) #1 (5) {
["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)
}


$data = ' [{' name ': ' A1 ', ' Number ': ' 123 ', ' Contno ': ', ' ' qqno ': '},{' Name ': ' A1 ', ' Number ': ' 123 ', ' Contno ': ' $ ', ' qqno ":" "},{" Name ":" A1 "," Number ":" 123 "," Contno ":" The "," Qqno ":"}] ';
echo Json_decode ($data);

The result is:

Array ([0] = StdClass Object ([Name] = + a1 [number] = 123 [Contno] [qqno] = =) [1] = Stdclas s Object ([name] = + a1 [number] = 123 [Contno] [qqno] = = [2] = = StdClass Object ([name] = a 1 [number] [123] [Contno] [qqno] [] =]

It can be seen that the object is compiled by Json_decode (), and now output Json_decode ($data, true) try

code as follows

echo Json_decode ($data, true);

knot Fruit:

array ([0] = = Array ([Name] + a1 [number] = 123 [Contno] = [Qqno] = [1] = = Array ([name] = a1 [number] = 123 [Contno] [qqno] = [2] = = Array ([name] = = = > 123 [Contno] [qqno] =)

You can see an associative array of Json_decode ($data, true) output, so Json_decode ($data) outputs an object, and Json_decode ("$arr", true) is forcing it to generate a PHP associative array.

If we get the JSON data as follows: (can be obtained using curl, fsockopen, etc.)

The code is as follows

{
"From": "zh",
"To": "en",
"Trans_result": [
{
"src": "u4f60u597d",
"DST": "Hello"
}
]
}

First, Json_decode returns an array in the same way:

Json_decode ($data, True), with the Json_decode function returning an array:

code as follows

array
(
[from] + zh
[to] = en
[trans_result] + Array
(
[0] = ARR Ay
(
[src] = Hello
[dst] = Hello
)

"

)

We can get the values we want in the PHP language in the following ways:

code as follows

!--? php
$data = << {
" from ":" zh ",
" to ":" en ",
" Trans_resul T ": [
{
" src ":" u4f60u597d ",
" DST ":" Hello "
}
]
}
STR;
$jsondata =json_decode ($data, true);
Header ("content-type:text/html; Charset=utf-8 ");
Print_r ($jsondata); Www.111cn.net
echo "
". $jsondata [' to ']; En
Echo "
". $jsondata [' Trans_result '][0][' DST ']; Hello
?

Second, the way Json_decode returns object:

Json_decode ($data);

The way to return an object with the Json_decode function gets:

code as follows

stdclass Object
(
[from] + zh
[to] = en
[trans_result] + Array
(
[0] = = StdClass Object
(
[src] = Hello
[DST] + Hello
)

"

)

We can get the values we want in the PHP language in the following ways:

The code is as follows

$data = <<<>
{
"From": "zh",
"To": "en",
"Trans_result": [
{
"src": "u4f60u597d",
"DST": "Hello"
}
]
}

STR;
$jsondata =json_decode ($data);
Header ("content-type:text/html; Charset=utf-8 ");
Print_r ($jsondata);
echo "
". $jsondata->from; Zh
echo "
". $jsondata->trans_result[0]->src; How are you doing
?>

http://www.bkjia.com/PHPjc/733190.html www.bkjia.com true http://www.bkjia.com/PHPjc/733190.html techarticle 1.json_decode () json_decode (PHP 5 = 5.2.0, PECL json = 1.2.0) Json_decode encode strings in JSON format mixed Json_decode (St Ring $json [, BOOL $assoc]) accept a ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.