Zend Framework processing JSON data Method _php Example

Source: Internet
Author: User
Tags json zend zend framework

This example describes the Zend Framework's approach to JSON data processing. Share to everyone for your reference, specific as follows:

JSON separator and its meaning

{} is used to implement the inclusion of the object, which is enclosed in curly braces
, commas are used to separate objects from different properties, or elements of an array
[] used to hold an array, and the array is stored in brackets
: Used to represent the value of a key/value pair, preceded by a key, and the value of the key after the colon

JSON sample

{'
  addressbook ': {'
    name ': ' Mary lebow ',
    ' address ': {
      ' street ': ' 5 Main Street ',
      ' city ': ' San Diego , CA ",
      zip": 91912
    },
    "Phonenumbers": [
      "619 332-3452",
      "664 223-4667"
    ]
  }
}

Using JSON

Syntax: $json = Zend_json::encode ($phpNative);
Description: The parameter $phpnative is a common type of data in PHP, it can be an array, an object, or another type of data.
The function return value is $json to a string that matches the JSON format.

Example:

<?php
require_once ("zend/json.php");
$temp = Array (
  "a" =>0, "
  B" =>1,
  "C" =>array (
    "c-1" =>21,
    "C-2" =>22,
    "c-3" = >23,
  ),
  "D" =>3
);
$json = Zend_json::encode ($temp);
echo "Temporary array contents are:";
echo "<pre>";
Print_r ($temp);
echo "</pre>";
echo "converts to JSON format content:";
echo "<pre>";
Print_r ($json);
echo "</pre>";

The results are:

The temporary array contents are:

array
(
  [a] => 0
  [b] => 1
  [c] => array
    (
      [c-1] =>
      [c-2 ] =>
      [c-3] =>
    )
  [d] => 3
)

converted to JSON format content:

{"A": 0, "B": 1, "C": {"c-1": 21, " C-2 ":", "c-3": "}," D ": 3}

Decoding JSON to normal data

Syntax:$phpNative = Zend_json::d ecode ($json);

Example:

<?php
require_once ("zend/json.php");
$json = "{
  \ Addressbook\": {
    \ "name\": \ "zhangsan\",
    \ "Address\": {
      \ "street\": \ "Chang an jie\",
      \ "city\": \ "beijing\",
      \ "zip\": 100001
    },
    \ "phonenumbers\": [
      \ "010-12345678\",
      \ " 010-11111111\ "
    ]
  }
}";
echo "before decoding:";
echo "<pre>";
Print_r ($json);
echo "</pre>";
$native = Zend_json::d ecode ($json);
echo "after decoding:";
echo "<pre>";
Print_r ($native);
echo "</pre>";

The output results are:

Before decoding:
{"
  addressbook": {"
    name": "Zhangsan",
    "address": {
      "street": "Chang a Jie",
      "City": " BeiJing ",
      " Zip ": 100001
    },
    " Phonenumbers ": [
      " 010-12345678 ",
      " 010-11111111 "
    ]
  }
}
After decoding:
array
(
  [addressbook] => array
    (
      [name] => Zhangsan [address
      ] => array
        (
          [Street] => Chang a Jie
          [city] => BeiJing
          [zip] => 100001
        )
      [phonenumbers] = > Array
        (
          [0] => 010-12345678
          [1] => 010-11111111))
)

Description

When you use this method to decode JSON content, you can decode it as an array or decode it as an object.

Specific Zend_json::d Ecode () the second parameter of the method is decided.

The syntax format is as follows

Phpnative=zendjson::d ecode (Phpnative=zendjson::d ecode (Json,zend_json::type_object);

The result of decoding the previous example to an object is

After decoding:

StdClass object
([
  addressbook] => StdClass object
    (
      [name] => Zhangsan
      [address ] => stdClass Object
        (
          [Street] => Chang a Jie
          [city] => BeiJing
          [zip] => 100001
        ) 
   [phonenumbers] => Array
        (
          [0] => 010-12345678
          [1] => 010-11111111
        )
)

Summary:

The use of JSON is fairly straightforward and requires JSON on the interface application. It can be used in different languages. The flexibility to pass data. The effect is similar to XML, but it saves more bandwidth than XML.

PS: A variety of common operations for JSON, but also refer to the use of this site related JSON online tools:

Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json

JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat

Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson

JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat

Online JSON compression/escape tool:
Http://tools.jb51.net/code/json_yasuo_trans

C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.