Home >Backend Development >PHP Tutorial >Detailed explanation of json array and json object and how to use $.post
In this article we mainly share with you the detailed explanation of json arrays and json objects and the method of using $.post. We hope it can help you. First, let’s take a look at a piece of code:
var jsonarray = eval('('+"[]"+')'); jsonarray.push( { "openid":"1", "operate_type":"sbbb" } );
First of all, we know that the outermost layer of the array is [], and the outermost layer of the object is {}.
The above code first creates a json array. There must be two brackets enclosed by single quotes in the eval function. Please refer to Baidu for the specific reason. And because the outermost layer of the array is [], the first line of code is "[]". In this way, the variable jsonarray can use the functions or operations of the array. Such as push. You only need to provide the correct string format of the json data in the push parameter, and the function will automatically convert it into array elements.
var jsonObject = eval('('+"{}"+')'); jsonObject.name = "zhang"; jsonObject.old = "18"; jsonObject.person = [{"name":"taylor","old":"22"},{"name":"taylor","old":"18"}]; console.log(jsonObject);
To create a json object, Because the outermost layer of the object is {}, so the first line It's "{}" in the code. In this way, the variable jsonObject can use the object's functions or operations. To set the key and value, just follow the second and third lines of code. If the value of a key is an array, then just give the correct string format of the json array.
##$.post method in jquery
$.post("{php echo web_url('management/management/set_admini');}", {group_id : group_id ,admini_to_set : jsonObject }, function(data){ alert(data); });When passing a json array or json object to the php background, use $admini_to_set = $_POST ['admini_to_set'] in php to take out the passed parameters, and find that: whether it is a json array or a json object, as long as you use The parameters taken out above will definitely become a php array.
Related recommendations:
$.post submits data and returns data in json format method example
javascript jQuery $.post $. Ajax usage
Simple example of $.post() method in jquery
The above is the detailed content of Detailed explanation of json array and json object and how to use $.post. For more information, please follow other related articles on the PHP Chinese website!