This function, the first alert(json[1].username); will prompt "candy". The json variable is an array object. So it needs to be called in the format of obj.username. The second alert(json2[0][0]); will prompt "crystal". The json2 variable is a complete json format. Both json and json2 variables achieve the same effect, but json2 is obviously much more streamlined than json. This is JavaScript's json format. Let's take a look at the json format in php. Let's look at a piece of code first:
In this code, $arr is an array, we use json_encode converts $arr to json format. This code will output: [{"catid":"4","catname":"u7a0bu7a0b","meta_title":"u7a0bu7a0bu535au5ba2"},{"catid":"6","catname":"climber","meta_title":"u6500u767bu8005 "}] This is how php handles json data. For json data, PHP can also use the json_decode() function to convert json data into an array. For example, in the above code, we use the json_decode function to process it. The above array will be printed out again.
Next, take a look at php json data and js How json data calls each other. Create a new file php_json.php:
Note: At the end of the php_json.php file, there is the sentence var jsonstr=;. This is to assign json format data to the jsonstr variable. Create another file json.html:
In this way, when viewing json.html, loadjson(jsonstr) will prompt "Rongrong ” and “climber”. This also implements js cross-domain calling. |