JSON in PHP

Source: Internet
Author: User
Tags php language json string format

1.json_decode ()

Json_decode

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

json_decode-strings encoded in JSON format

Description

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

Accepts a JSON-formatted string and converts it to a PHP variable

Parameters

Json

A string in JSON string format to be decoded.

Assoc

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

return value

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

Example

Examples of Example #1 Json_decode ()

  code is as follows &nbs P;

<?php 
$json = ' {' A ': 1, "B": 2, "C": 3, "D": 4, "E": 5} ';   ;
Var_dump (Json_decode ($json)),  
Var_dump (Json_decode ($json, True)),  
?

The previous example will output:

Object (StdClass) #1 (5) { 
["a"] => int (1)  
["B"] => int (2)  
["C"] => int (3)  < br> ["D"] => int (4)  
["E"] => int (5)  
}

Array (5) { 
["a"] => int (1)  
[ "B"] => int (2)  
["C"] => int (3)  
["D"] => int (4)  
["E"] => int (5)  
}

p>
$data = ' [{' Name]: ' A1 "," Number ":" 123 "," Contno ":" "," qqno ":" "},{" Name ":" A1 "," Number ":" 123 "," Contno ":" 000 " , "qqno": ""},{"Name": "A1", "Number": "123", "Contno": "V", "qqno": ""}] ';  
Echo Json_decode ($data);

The

result is:

Array ([0] => StdClass ObjECT ([name] => A1 [number] => 123 [Contno] => [qqno] =>) [1] => stdClass Object ([name] => A1 [Nu Mber] => 123 [Contno] => [qqno] =>) [2] => stdClass Object ([Name] => A1 [number] => 123 [Contno] => [qqno] =>))

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

The code is as follows

Echo Json_decode ($data, true);

Results:

Array ([0] => Array ([name] => A1 [number] => 123 [Contno] => [qqno] =>) [1] => Array ([name] =& Gt A1 [number] => 123 [Contno] => [qqno] =>) [2] => Array ([Name] => A1 [number] => 123 [Contno] => ; [Qqno] =>)

You can see an associative array of Json_decode ($data, true) output, so that Json_decode ($data) output is an object, and Json_decode ("$arr", true) forces 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": "en",
"To": "en",
"Trans_result": [
{
"src": "u4f60u597d",
"DST": "Hello"
}
]
}

One, Json_decode return array way:

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

  code is as follows &nbs P;

Array
(
    [from] => en
   ;  [to] => en
    [trans_result] => Array
        (
            [0] => Array
                 (
                     [src] => Hello
                     [ DST] => Hello
               )

       )

)

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

The code is as follows

<?php
$data = <<<str
{
"From": "en",
"To": "en",
"Trans_result": [
{
"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 "<br/>". $jsondata [' to ']; En
echo "<br/>". $jsondata [' Trans_result '][0][' DST ']; Hello
?>

Second, the way Json_decode returns object:

Json_decode ($data);

Returns the object by using the Json_decode function:

  code is as follows &nbs P;

StdClass Object
(
    [from] => en
& nbsp;   [to] => en
    [trans_result] => Array
     & nbsp;  (
            [0] => stdClass Object
                (
                     [src] = > Hello
                     [DST] => Hello
                )

       )/p>

)

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

The code is as follows

<?php
$data = <<<str
{
"From": "en",
"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 "<br/>". $jsondata->from; Zh
echo "<br/>". $jsondata->trans_result[0]->src; How are you doing
?>

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.