CONVERT JSON TO OBJECT PHP


A common use of JSON is lớn read data from a web server, and display the data in a web page.

Bạn đang xem: Convert json to object php

This chapter will teach you how khổng lồ exchange JSON data between the client và a PHP server.

The PHP File

PHP has some built-in functions lớn handle JSON.

Objects in PHP can be converted into JSON by using the PHP function json_encode() :



name = "John";$myObj->age = 30;$myObj->city = "New York";$myJSON = json_encode($myObj);echo $myJSON;?>

The Client JavaScript

Here is a JavaScript on the client, using an AJAX call to request the PHP tệp tin from the example above:



const xmlhttp = new XMLHttpRequest(); xmlhttp.onload = function() xmlhttp.open("GET", "demo_file.php"); xmlhttp.send();

PHP Array

Arrays in PHP will also be converted into JSON when using the PHP function json_encode() :





The Client JavaScript

Here is a JavaScript on the client, using an AJAX gọi to request the PHP tệp tin from the array example above:



var xmlhttp = new XMLHttpRequest(); xmlhttp.onload = function() xmlhttp.open("GET", "demo_file_array.php", true); xmlhttp.send();

PHP Database

PHP is a vps side programming language, and can be used lớn access a database.

Xem thêm: How To Select An Option By Its Option Text Using Jquery ? How To Get Selected Text From A Drop

Imagine you have a database on your server, và you want khổng lồ send a request khổng lồ it from the client where you ask for the 10 first rows in a table called "customers".

On the client, make a JSON object that describes the numbers of rows you want lớn return.

Before you send the request khổng lồ the server, convert the JSON object into a string & send it as a parameter lớn the url of the PHP page:



const limit = ; const dbParam = JSON.stringify(limit); xmlhttp = new XMLHttpRequest(); xmlhttp.onload = function() xmlhttp.open("GET","json_demo_db.php?x=" + dbParam); xmlhttp.send();

Example explained:

Define an object containing a "limit" property and value. Convert the object into a JSON string. Send a request to the PHP file, with the JSON string as a parameter. Wait until the request returns with the result (as JSON) Display the result received from the PHP file.

Take a look at the PHP file:



, false); $conn = new mysqli("myServer", "myUser", "myPassword", "Northwind"); $stmt = $conn->prepare("SELECT name FROM customers LIMIT ?");$stmt->bind_param("s", $obj->limit);$stmt->execute();$result = $stmt->get_result(); $outp = $result->fetch_all(MYSQLI_ASSOC);echo json_encode($outp);?>

PHP file explained:

Convert the request into an object, using the PHP function json_decode() . Access the database, và fill an array with the requested data. địa chỉ cửa hàng the array lớn an object, và return the object as JSON using the json_encode() function.

Use the Data



xmlhttp.onload = function()   document.getElementById("demo").innerHTML = text; }

PHP Method = POST

When sending data lớn the server, it is often best lớn use the HTTP POST method.

To send AJAX requests using the POST method, specify the method, & the correct header.

The data sent khổng lồ the hệ thống must now be an argument khổng lồ the send() method:



const dbParam = JSON.stringify(); const xmlhttp = new XMLHttpRequest(); xmlhttp.onload = function()   document.getElementById("demo").innerHTML = text; } xmlhttp.open("POST", "json_demo_db_post.php"); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send("x=" + dbParam);



$obj = json_decode($_POST<"x">, false); $conn = new mysqli("myServer", "myUser", "myPassword", "Northwind"); $stmt = $conn->prepare("SELECT name FROM customers LIMIT ?");$stmt->bind_param("s", $obj->limit);$stmt->execute();$result = $stmt->get_result(); $outp = $result->fetch_all(MYSQLI_ASSOC);echo json_encode($outp);?>
×

Report Error

If you want to report an error, or if you want lớn make a suggestion, bởi not hesitate lớn send us an e-mail:

help


top Tutorials HTML Tutorial CSS Tutorial JavaScript Tutorial How lớn Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
vị trí cao nhất References HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference Angular Reference jQuery Reference
đứng đầu Examples HTML Examples CSS Examples JavaScript Examples How lớn Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples
website Courses HTML Course CSS Course JavaScript Course Front end Course SQL Course Python Course PHP Course jQuery Course Java Course C++ Course C# Course XML Course Get Certified »
obatambeienwasirherbal.com is optimized for learning & training. Examples might be simplified lớn improve reading và learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using obatambeienwasirherbal.com, you agree to lớn have read & accepted our terms of use, cookie và privacy policy. Copyright 1999-2022 by Refsnes Data. All Rights Reserved. obatambeienwasirherbal.com is Powered by W3.CSS.