How to invoke JSON's key name as a number (sample code) _php Tips

Source: Internet
Author: User
Tags php json

You must use OBJ[XX to obtain a value for a key that is a numeric or characters variable character, such as a space.

Copy Code code as follows:

<?php

Declaring JSON data
$array = Array (' Result ' =>array ("=>" "90 Queues", "status" => "Success"));
$json = Json_encode ($array);

$array 1 = Array ("=>", "90 Queues", "status" => "Success");
$json 1 = json_encode ($array 1);
$phpjson = Json_decode ($json 1,true);//The second argument is true, which means converting the JSON data to an array
For JSON key names are numbers, you can only handle $phpjson[' 90 ' by array;
?>
<! DOCTYPE unspecified public "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<body>
<script type= "Text/javascript" >
/**
* Test JSON data invocation sample
*/
function Test () {
Call Way One
var data = ' <?php echo $json?> ';//php json, only single quotes here, because PHP has double quotes in its JSON data.
data = eval ("(+data+)");//js parse JSON data, mainly because the JSON data becomes a string after single quotes
Alert (data[' result '][90]);//For numbers need to be accessed in array mode
Alert (data[' result '].status)://For non-digits can be used to access

Call Mode Two
var data1 = <?php echo $json 1?>;//php JSON data, there is no single quotation mark, because it is directly JSON data
Alert (data1[90]);//For numbers need to be accessed in array mode
alert (data1.status);//For non-digits can be used to access
Alert (data1[' status ');//can also be called using an array method

Note: You must use OBJ[XX to get the value for the key name is numeric or characters variable character (if there is a space).
}
</script>
<input type= "button" value= "button" onclick= "Test ();" />
</body>

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.