PHP JSON Handling
In this page:
$json = json_encode($data); // PHP value to JSON string
$object = json_decode($json); // to stdClass object
$array = json_decode($json, true); // to associative array
Data को JSON में Encode करना
json_encode() PHP arrays या objects को एक JSON string में बदल देता है, जो एक JavaScript frontend या किसी third-party API को structured data भेजने का standard format है।
उदाहरण: Encoding Data to JSON
<?php
// Declare `$data` as an array: `["name" => "Alice", "age" => 30]`
$data = ["name" => "Alice", "age" => 30];
// Print `json_encode($data)` to the output
echo json_encode($data);
?>
Login to try C/C++/Java/PHP code in the editor
JSON को Objects में Decode करना
json_decode() एक JSON string को वापस PHP values में parse करता है; default रूप से यह nested stdClass objects produce करता है, इसलिए आप array bracket syntax के बजाय -> से fields access करते हैं।
उदाहरण: Decoding JSON to Objects
<?php
// Declare `$json`, set to '{"name": "Alice", "age": 30}'
$json = '{"name": "Alice", "age": 30}';
// Declare `$obj`, set to `json_decode($json)`
$obj = json_decode($json);
// Print `$obj->name` to the output
echo $obj->name;
?>
Login to try C/C++/Java/PHP code in the editor
JSON को Associative Arrays में Decode करना
json_decode() के दूसरे argument की तरह true pass करना same JSON को objects के बजाय nested associative arrays में decode करता है, जो कई developers को PHP में काम करने के लिए ज़्यादा convenient लगता है।
उदाहरण: Decoding JSON to Associative Arrays
<?php
// Declare `$json`, set to '{"name": "Alice", "age": 30}'
$json = '{"name": "Alice", "age": 30}';
// Declare `$arr`, set to `json_decode($json, true)`
$arr = json_decode($json, true);
// Print `$arr['name']` to the output
echo $arr['name'];
?>
Login to try C/C++/Java/PHP code in the editor
JSON Errors Handle करना
Malformed JSON json_decode() को चुपचाप null return करवाता है, इसलिए decoded result पर भरोसा करने से पहले json_last_error() check करना (या human-readable reason के लिए json_last_error_msg() पढ़ना) ज़रूरी है।
उदाहरण: Handling JSON Errors
<?php
// Declare `$json`, set to '{invalid json}'
$json = '{invalid json}';
// Declare `$result`, set to `json_decode($json)`
$result = json_decode($json);
// Check whether `json_last_error() !== JSON_ERROR_NONE`
if (json_last_error() !== JSON_ERROR_NONE) {
// Print `"Error: " . json_last_error_msg()` to the output
echo "Error: " . json_last_error_msg();
}
?>
Login to try C/C++/Java/PHP code in the editor
JSON Encoding Options
json_encode() को JSON_PRETTY_PRINT flag pass करना output में indentation और line breaks add करता है, जो API responses debug करने के लिए उपयोगी है लेकिन production में छोटे payloads के लिए आमतौर पर skip किया जाना चाहिए।
उदाहरण: JSON Encoding Options
<?php
// Declare `$data` as an array: `["name" => "Alice", "age" => 30]`
$data = ["name" => "Alice", "age" => 30];
// Print `json_encode($data, JSON_PRETTY_PRINT)` to the output
echo json_encode($data, JSON_PRETTY_PRINT);
?>
Login to try C/C++/Java/PHP code in the editor
json_decodeके बादjson_last_error()check न करना, ताकि invalid JSON चुपचाप null return करे।- एक associative array पाने के लिए दूसरा argument
trueभूल जाना, और फिर एक object पर array syntax इस्तेमाल करना। - ऐसा text encode करना जो valid UTF-8 नहीं है, जो
json_encodeको false return करवाता है।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser