PHP Json

PHP

PHP JSON (JavaScript Object Notation) एक standard lightweight डेटा-इंटरचेंज (storing and exchanging) format है, जो जेनरेट करने में आसान और तेज़ है।

चूंकि JSON एक text-based format है, इसलिए मनुष्यों और कंप्यूटर दोनों के लिए लिखना और समझना आसान है। इसे आसानी से सर्वर से भेजा जा सकता है और किसी भी programming language द्वारा, डेटा के format में उपयोग किया जा सकता है।

Encoding PHP JSON Data

PHP में json_encode() फ़ंक्शन का उपयोग JSON format में वैल्यू को एन्कोड करने के लिए किया जाता है।

Syntax
string json_encode($value [, $options = 0 ])

Example Associative array

नीचे दिए गए उदाहरण से पता चलता है कि PHP associative array को JSON ऑब्जेक्ट में कैसे एन्कोड किया जाता है।

Example
<?php
$marks = array("Narender" => 85, "Nilesh" => 92, "Brajlal" => 48);
echo json_encode($marks);
?>
Output
{"Narender":85,"Nilesh":92,"Brajlal":48}

Example Indexed array

नीचे दिया गया उदाहरण दिखाता है कि किसी indexed array को JSON array में कैसे एन्कोड किया जाए।

Example
<?php
$pc_parts = array("Mouse", "Keyboard", "CPU", "Monitor");
echo json_encode($pc_parts);
?>
Output
["Mouse","Keyboard","CPU","Monitor"]

Example PHP object

नीचे दिया गया उदाहरण दिखाता है कि PHP ऑब्जेक्ट्स को JSON में कैसे एन्कोड किया जाए।

Example
<?php
class Example{
    public $name, $hobbies, $birthday = "";
}

$x = new Example();
$x->name = "Brajlal";
$x->hobbies = "Cooking";
$x->birthday = date("d-M-Y", strtotime("5/17/1991"));

echo json_encode($x);
?>
Output
{"name":"Brajlal","hobbies":"Cooking","birthday":"17-May-1991"}

Decoding PHP JSON Data

PHP में json_decode() फ़ंक्शन का उपयोग JSON format में वैल्यू को डीकोड करने के लिए किया जाता है। यह फ़ंक्शन JSON से डिकोड किए गए वैल्यू को उपयुक्त PHP type में लौटाता है।

Syntax
mixed json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]])

Paramaters

  • json_string: यह एक encoded string है जो UTF-8 encoded होना चाहिए।
  • assoc: यह एक boolean type पैरामीटर होता है, जब TRUE सेट किया जाता है तो यह returned objects को associative arrays में बदल देता है। यदि खाली छोड़ दिया जाता है तो डिफ़ॉल्ट रूप से false होता हैं।
  • depth: यह एक integer type का पैरामीटर है जो recursion depth को specifies करता है।
  • options: यह JSON decode का एक integer type bitmask है, JSON_BIGINT_AS_STRING समर्थित हैं।

Example PHP decode Object

नीचे दिया गया उदाहरण दिखाता है कि बिना assoc पैरामीटर के JSON ऑब्जेक्ट्स को कैसे डिकोड किया जाए जो डिफ़ॉल्ट रूप से false हैं।

Example
<?php
$marks = '{"Narender":85, "Nilesh":92, "Brajlal":48}';
var_dump(json_decode($marks));
?>
Output
object(stdClass)#1 (3) { ["Narender"]=> int(85) ["Nilesh"]=> int(92) ["Brajlal"]=> int(48) }

Example decode PHP Object with true

नीचे दिया गया उदाहरण दिखाता है कि assoc पैरामीटर के JSON ऑब्जेक्ट्स को कैसे डिकोड किया जाए, जब पैरामीटर true हो।

Example
<?php
$marks = '{"Narender":85, "Nilesh":92, "Brajlal":48}';
var_dump(json_decode($marks, true));
?>
Output
array(3) { ["Narender"]=> int(85) ["Nilesh"]=> int(92) ["Brajlal"]=> int(48) }

PHP JSON के बारे में अधिक जानने के लिए, कृपया देखें https://www.php.net/manual/en/book.json.php

Article By: Brajlal Prasad
Created on: 17 Feb 2023  1020  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]