About the Zend Framework's approach to working with JSON data

Source: Internet
Author: User
Tags zend zend framework
This article mainly introduced the Zend Framework processing JSON data method, combined with the example form analysis of the Zend Framework for the JSON-related operation class, the need for friends can refer to the following

This example describes how the Zend Framework processes JSON data. Share to everyone for your reference, as follows:

JSON separators and meanings

{} is used to implement the object's inclusion, and the object is enclosed in curly braces
, commas are used to separate different properties of an object, or elements of an array
[] to hold the array, the array will be stored in brackets
: A value that represents a key/value pair, a key before a colon, and a value for the key after a 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: Where the parameter $phpnative is a common data type for PHP, it can be an array, an object, or other type of data.
The function return value is $json to a string that conforms to the JSON format.

Example:

<?phprequire_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> ";p Rint_r ($temp); echo" </pre> "echo" is converted to JSON format: "; echo" <pre> ";p Rint_r ($json); echo "</pre>";

The result is:

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 is: {"A": 0, "B": 1, "C": {"c-1":, "c-2":, "c-3": +}, "D": 3}

Decoding JSON to normal data

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

Example:

<?phprequire_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> ";p Rint_r ($json); echo" </ Pre> "; $native = Zend_json::d ecode ($json); echo" after decoding: "; echo" <pre> ";p Rint_r ($native); echo" </pre> ";

The output is:

Before decoding: {  "AddressBook": {"    name": "Zhangsan",    "address": {      "street": "Chang an Jie", "City      ": " Beijing ",      " Zip ": 100001    },    " Phonenumbers ": [      " 010-12345678 ",      " 010-11111111 "    ]  }} After decoding: Array (  [addressbook] = = Array    (      [Name] = + Zhangsan      [address] = = Array        (          [Street] = Chang an Jie          [city] = Beijing          [zip] = 100001        )      [phonenumbers] = Array
   (          [0] = 010-12345678          [1] = 010-11111111        )    ))

Description

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

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

The syntax format is as follows

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

The result of the previous example decoding to an object is

After decoding: StdClass object (  [AddressBook] = StdClass object    (      [name] = Zhangsan      [Address] = StdClass Object        (          [Street] = Chang an Jie          [city] = Beijing          [zip] = 100001        )      [ Phonenumbers] = Array        (          [0] = 010-12345678          [1] = 010-11111111        )    ))

Summary:

The use of JSON is still relatively simple and requires JSON for interface applications. It can be used in different languages. Data can be transferred flexibly. Works like XML, but saves bandwidth over XML.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.