Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> JSON >> How to use JSON with PHP
How to use JSON with PHP
How to use JSON with PHP


by Arun Singh on 5/8/2012 5:28:55 PM

Views: 1266       Comments: 0

How to use JSON with PHP

This article is going to explain on how to use JSON with PHP.  PHP 5.2.0 supports JSON out of the box. So, when you install PHP, JSON support is automatically installed and you don't need any installation and configuration in addition.

JSON supports three PHP functions: json_decode, json_encode and json_last_error. We will discuss all of these functions with examples in the following section of this page. Let’s take a brief idea about these functions.

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

Where,

$json ->   The json string being decoded. This function only works with UTF-8 encoded data.

$assoc -> When TRUE, returned objects will be converted into associative arrays.

$depth -> User specified recursion depth.

$options -> Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)

Json_decode:

json_decode() function decodes a JSON string. Suppose you have obtained some data in JSON format and you want to convert it into PHP variable for the purpose of presenting that data to user or use it for further programming, you have to use this function.

Example:  In this example I’m just creating a function with GetJsonFeed() which return the decode value of passed string.

<!--

To change this template, choose Tools | Templates

and open the template in the editor.

-->

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

        <title>JSON | Decode | Example</title>

    </head>

    <body>

        <?php

            function GetJsonFeed($url)

             {

                $feed = file_get_contents($url);

                return json_decode($feed);

             }

             $tweets = GetJsonFeed("http://www.mindstick.com");

           

        ?>

        <p>

            <?php

                 echo $tweets -> text;

            ?>

        </p>

    </body>

</html>

 

Json_encode:

Json_encode returns a string containing the JSON representation of value. This function is used to transform array and objects to JSON format.

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

Where,

$value -> Represents the value to encode;

$options -> Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE.

Example: In this example, simply I’m creating a one dimensional array object and pass this to json_encode function.

<?php

             // Declare object of one dimensional array

                $arr = array('a1' => 1, 'b1' => 2, 'c1' => 3, 'd1' => 4, 'e1' => 5);

               // Encode this array object

                echo json_encode($arr);

       ?>

 

Json_last_error:

While working on encoding or decoding JSON, if an error occur, json_last_error() function returns the last error. Json_last_error() function returns the integer value.

Syntax: int json_last_error();

This is the complete description on JSON functions which is widely used in PHP or how we can use JSON with PHP.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Arun SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 7259
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.