JSON is one of the popular data formats across all technologies. JSON handling with PHP is easier than it looks. Most of the APIs uses JSON format for data interchange. For example, if you want lớn extract profile data from Facebook using its API, it returns the data in JSON format. There is no option khổng lồ ignore JSON. 


The JSON object contains an associative array of “name:value” pairs whereas the JSON array contains a sequence of values with default numeric indexes.Bạn đã xem: Json php

The JSON is evolved from the JavaScript và ECMAScript programming language. Standard ECMA-404 contains the specification for a valid JSON syntax.

Bạn đang xem: How to parse json from string with data have special chars

Example of a JSON Object và Array

In the JSON introduction, we saw that the format can be varied based on the data structure. Let us see the examples for these two JSON formats object & array.

Example of a valid JSON Object

JSON object format consists of a collection of key-value pairs of data. Below structure shows the example of a valid JSON object structure. 

"FIFA_World_Cup_Winners" : "2018" : "France", "2014" : "Germany", "2010" : "Spain", "2006" : "Italy", "2002" : "Brazil"

Example of a JSON Array

In a JSON array, the sequence of data will be listed in an ordered list. There will be no key-value mapping. The following example shows a JSON array structure.

"FIFA_Last_Five_Winners" : In the above example JSON, the last five winner countries are listed without explicit indexes. So it will be treated as an array of ordered danh sách of items.

JSON array will be enclosed with whereas the JSON objects are enclosed with curly brackets

JSON Handling with PHP by Parsing tệp tin Data 

With the reference about the possible JSON structure, now we are going to see about JSON handling with PHP to lớn parse data from an đầu vào file containing JSON data as shown below.

"FIFA_World_Cup_finals":Pass this file path to the PHP file_get_contents() function and store the JSON data into a variable.

As the input đầu vào JSON tệp tin contains multi-level hierarchical structural data, we have to recursively iterate the JSON object using PHP RecursiveArrayIterator.

PHP’s RecursiveArrayIterator simplifies the JSON parsing implementation with few lines of code as shown below. You should realize và make use of the power nguồn of PHP built-in functions.

$val) if(!is_array($val)) if($key == "Year") print ""; print $key." : ".$val . ""; ?>Then, this PHP JSON parsing program will return the following output đầu ra to the browser.


How khổng lồ Decode JSON to lớn Array

In a previous tutorial, we have seen how to encode-decode JSON using PHP. The json_encode & json_decode PHP functions are used khổng lồ perform the encode and decode operations respectively.

The following code block shows the syntax of the json_decode function. This function accepts JSON string input đầu vào as its first parameter. Also, it accepts an optional boolean value as its second parameter.

Consider the following input JSON data. Let us see how it is decoded into an array using json_decode() function.

";print_r($outputArray);?>The output đầu ra array is,


Decode JSON to Object

By default, the PHP json_decode function will convert the JSON data into an object. The $assoc parameter of the json_decode function will force the output format based on the boolean value passed lớn it.


JSON to lớn Object Conversion Output


Convert PHP Array to lớn JSON

For converting a PHP array to a JSON format, json_encode() function is used. Following code snippet specifies the PHP json_encode() function’s syntax.

Xem thêm: 12 Cung Hoàng Đạo Trong Kamen Rider Fourze, Kamen Rider Fourze

json_encode ( mixed $value > ) : stringThis function accepts the JSON encoded input as its first parameter. It should mandatorily be specified with a JSON string while invoking json_encode to process the data conversion.

Below PHP script is used lớn convert a PHP array into a JSON format. In this example, I have used an associative array which will be sent to lớn the json_encode function. 

array ( "Year" => "2018", "data" => array ( "Winner" => "France", "Score" => "4-2", "Runner-up" => "Croatia") ) );$encodedJSON = json_encode($inputArray, JSON_PRETTY_PRINT);print($encodedJSON);?>By running this program, the encoded JSON data will be printed on the browser as shown below.

"FIFA_Last_World_Cup_final": "Year": "2018", "data": "Winner": "France", "Score": "4-2", "Runner-up": "Croatia" Note:

PHP JSON encode decode functions will work with UTF-8 formatted strings.

Read JSON via AJAX & Process

In this section, we are going to lớn see about JSON handling with PHP and AJAX. I have used JavaScript to lớn send the AJAX điện thoại tư vấn to the PHP via an XML HTTP request.

Add the below script on the HTML page from where you want to access JSON data returned by PHP. 

The AJAX callback will get the response JSON from the PHP endpoint. In this example, the PHP endpoint is specified as the JavaScript AJAX URL by setting the action parameter in the URL query string. It calls the getJSON.php file via AJAX.

The returned JSON output đầu ra is received in the onreadystatechange callback. This đầu ra data is parsed with the JSON.parse method of the JavaScript.

In the PHP script, the JSON data is returned lớn the AJAX by using PHP print statement. Before creating the output đầu ra JSON data, it checks the required kích hoạt parameter sent via AJAX is not empty.

array ( "Year" => "2018", "data" => array ( "Winner" => "France", "Score" => "4-2", "Runner" => "Croatia") ) );$encodedJSON = json_encode($inputArray, JSON_PRETTY_PRINT);print($encodedJSON);exit;}?>This AJAX script process the JSON response in the success callback function. In this callback, the read JSON data are used to lớn update the UI. The output will be displayed as below.


How to Access a JSON Feed and Display Data

Accessing JSON feed URL can be done in various ways. In the above example on parsing JSON tệp tin data via PHP, we have used file_get_contents() function. 

The file_get_contents() will not work on the server because of the security directives enable with the PHP.ini configurations. So, we can also use CURL script lớn extract JSON data from the remote feed URL.

The following example will show how lớn use PHP CURL lớn access JSON feed & display data lớn the browser.


How khổng lồ Convert JSON to lớn JavaScript Object

We have already seen how lớn convert JSON to JavaScript Object while creating the example on JSON handling with PHP via AJAX without jQuery.

In this section, we are going to lớn see how to lớn convert JSON data into a JavaScript Object. Also, we have to loop through the resultant Javascript Object array.

JSON khổng lồ JavaScript Object conversion can be done by using various ways. As lượt thích as the previous example, we can vì this by using JavaScript’s built-in JSON.parse() method. 

Using jQuery the $.parseJSON method will support this conversion và make it working in many obsolete web browsers. The following code uses jQuery to convert a JSON input đầu vào string into a JavaScript Object.