Home  >  Article  >  Web Front-end  >  Zend Framework processing Json data method

Zend Framework processing Json data method

不言
不言Original
2018-05-07 09:35:58935browse

This article mainly introduces the Zend Framework's method of processing Json data, and analyzes the use of zend framework for json-related operation classes in the form of examples. Friends in need can refer to the following

The examples of this article describe Zend Framework Methods for processing Json data. Share it with everyone for your reference, the details are as follows:

JSON delimiters and meaning

##{} is used to implement the inclusion of objects, and the objects are included in the large in brackets, commas are used to separate different attributes of an object, or elements of an array
[] are used to store arrays, and arrays will be stored in square brackets
: Used to represent the value of a key/value pair, The key is before the colon, and the value after the colon is the key

JSON example

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

Use JSON

Syntax: $json = Zend_Json::encode($phpNative);

Description: Among them, the parameter $phpNative is a common data type in PHP, which can be an array, object or other types of data.
The function return value $json is a string that conforms to 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 "临时数组内容为:";
echo "<pre class="brush:php;toolbar:false">";
print_r($temp);
echo "
"; echo "转换为JSON格式内容为:"; echo "
";
print_r($json);
echo "
";

The result is:

临时数组内容为:

Array
(
  [a] => 0
  [b] => 1
  [c] => Array
    (
      [c-1] => 21
      [c-2] => 22
      [c-3] => 23
    )
  [d] => 3
)

转换为JSON格式内容为:

{"a":0,"b":1,"c":{"c-1":21,"c-2":22,"c-3":23},"d":3}

Decode JSON into normal data

Syntax:

$phpNative = Zend_Json::decode($json);

Example:

";
print_r($json);
echo "
"; $native = Zend_Json::decode($json); echo "解码后为:"; echo "
";
print_r($native);
echo "
";

The output result is:

解码前为:
{
  "addressbook":{
    "name":"zhangsan",
    "address":{
      "street":"Chang an jie",
      "city":"BeiJing",
      "zip":100001
    },
    "phoneNumbers":[
      "010-12345678",
      "010-11111111"
    ]
  }
}
解码后为:
Array
(
  [addressbook] => Array
    (
      [name] => zhangsan
      [address] => Array
        (
          [street] => Chang an jie
          [city] => BeiJing
          [zip] => 100001
        )
      [phoneNumbers] => Array
        (
          [0] => 010-12345678
          [1] => 010-11111111
        )
    )
)

Explanation:

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

The details are determined by the second parameter of the Zend_Json::decode() method.

The syntax format is as follows

phpNative=ZendJson::decode(phpNative=ZendJson::decode(json,Zend_Json::TYPE_OBJECT);

Up The result after decoding an example into an object is

解码后为:

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 relatively simple. Json is required for interface applications. It can be used in different languages. It has a similar function to XML, but it saves bandwidth.

Related recommendations:


How to implement Ajax in the Zend Framework

Zend Framework's method of establishing a ZF project based on the Command line

The above is the detailed content of Zend Framework processing Json data method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn