json is called when the key name is a number of sample code

  • 2020-11-26 18:43:35
  • OfStack

obj[xx] must be used to get the value when the key name is a number or an unusual variable character (if there are Spaces).


<?php
// The statement json data 
$array = array('result'=>array("90"=>"90 The queue ","status"=>" successful "));
$json = json_encode($array);
$array1 = array("90"=>"90 The queue ","status"=>" successful ");
$json1 = json_encode($array1);
$phpjson = json_decode($json1,true);// The first 2 A parameter is true , said the json The data is converted to an array 
// for json When the key name is a number, it can only be handled as an array $phpjson['90'];
?>
<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<script type="text/javascript">
/**
*  test json Data invocation example 
*/
function test(){
// Call way 1
var data = '<?php echo $json?>';//php json data , I can only use single quotes here because php the json There are double quotes in the data 
data = eval("("+data+")");//js  parsing json data , Mainly because json The data is turned into a string with single quotes 
alert(data['result'][90]);// The Numbers need to be accessed as arrays 
alert(data['result'].status);// Can be used for non-numbers . Access by means of 
// Call way 2
var data1 = <?php echo $json1?>;//php json The data, I'm not using single quotes here, because it's direct json data 
alert(data1[90]);// The Numbers need to be accessed as arrays 
alert(data1.status);// Can be used for non-numbers . Access by means of 
alert(data1['status']);// You can also call it using an array 
// Note: If the key name is a number or an unusual variable character (such as a space), it must be used obj[xx] Method gets the value. 
}
</script>
<input type="button" value="button" onclick="test();"/>
</body>
</html>


Related articles: