PHP json_decode()

📔 : PHP 🔗

json_decode() function ,JSON string को decode करता है।

PHP json_decode Syntax

json_decode ( string $json , bool|null $associative , int $depth , $json_constants);

Parameters
  1. string $json | required : value जिसे आप decode करना चाहते हैं।

  2. bool|null $associative | oprional : यह Boolean value है , true pass करने पर हमें JSON objects as a Associative Array रेतुर्न होगा। और false pass करने पर JSON objects as a Object return होगा। By Default false होता है।

  3. int $depth | optional : यह maximum depth specify करती है , जिसकी value 0 से ज्यादा ही होनी चाहिए।

  4. $json_constants | optional : यह JSON Constants , जिसके according value JSON format में convert होती है। कुछ JSON Constants इस प्रकार हैं -

    JSON Contstans
    JSON_FORCE_OBJECT, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_THROW_ON_ERROR.

  5. Return Value : PHP value के type के according decoded value return होती है। और अगर pass की गयी value decode न हो पायी तो null return होता है।

PHP json_decode Example

File : php_json_decode.php

Copy Fullscreen Close Fullscreen
<?php 

  $json = '{
         "course":"Computer Science", 
         "no_of_subject":4, 
         "subjects":"PHP , JavaScript , jQuery , AJAX"
  }';
  var_dump(json_decode($json));
  /*now , pass true so that you can see difference*/
  var_dump(json_decode($json, true));
?>
Output
object(stdClass)#1 (3) {
  ["course"]=>
  string(16) "Computer Science"
  ["no_of_subject"]=>
  int(4)
  ["subjects"]=>
  string(32) "PHP , Javascript , jQuery , AJAX"
}
array(3) {
  ["course"]=>
  string(16) "Computer Science"
  ["no_of_subject"]=>
  int(4)
  ["subjects"]=>
  string(32) "PHP , Javascript , jQuery , AJAX"
}

Important
var_dump() function variable(s) के structure / information के बारे में बताता है। यह function variable में stored value उसका type , length etc. बताता है।
Learn More About var_dump()

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook