search
Homephp教程php手册php输出json格式数据的例子

php直接输出json格式,很多新手有一个误区,以为用echo json_encode($data);这样就是输出json数据了,没错这样输出文本是json格式文本而不是json数据,正确的写法是应该加一句,我们要在利用Content-type:text/json才可以正常显示是json数据.

PHP实例代码如下:

<?php 
header(&#39;Content-type:text/json&#39;);//这句是重点,它告诉接收数据的对象此页面输出的是json数据; 
$json={"n":"name","p":"password"};//虽然这行数据形式上是json格式,如果没有上面那句的话,它是不会被当做json格式的数据被处理的; 
echo $json;   
?>

json_encode当然也可以输入json字符串了,下面看几个例子.

PHP生成JSON的函数是:json_encode($PHPcode);

PHP解析JSON的函数是:json_decode($JSONcode);

所以JSON的形式有多种,不同的形式在PHP解释出来后的形式也是不同的.

形式1:完全是对象的形式,这种形式的数据在Javascript中又叫相关数组,与一般数组不同的是,它可以通过字符串作索引来访问(用"[]"或"."来表示层级),代码如下:

$json=&#39;{"item1":{"item11":{"n":"chenling","m":"llll"},"sex":"www.phprm.com","age":"25"},"item2":{"item21":"ling","sex":"女","age":"24"}}&#39;; 
$J=json_decode($json); 
print_r($J);  
/*将输出: 
stdClass Object 
( 
  [item1] => stdClass Object 
  ( 
   [item11] => stdClass Object 
   ( 
     [n] => chenling 
     [m] => llll 
   ) 
 
   [sex] => www.phprm.com 
   [age] => 25 
  ) 
 
  [item2] => stdClass Object 
  ( 
   [item21] => ling 
   [sex] => 女 
   [age] => 24 
  ) 
)*/

比如说我要取得了值是chenling的那个属性,则应该这样访问:

$J->item1->item11->n;//这将取得属性n的值:chenling

其实这种访问形式跟访问普通的对象属性差不多,也相当于访问一个3维数组.

形式2:对象和数组混合,代码如下:

$json=&#39;{"item1":[{"name":[{"chen":"chenling","ling":"chenli"}],"sex":"男","age":"25"},{"name":"sun","sex":"女","age":"24"}]}&#39;; 
$J=json_decode($json); 
print_r($J);  
/*将输出: 
stdClass Object 
( 
  [item1] => Array 
  ( 
   [0] => stdClass Object 
   ( 
     [name] => Array 
     ( 
       [0] => stdClass Object 
       ( 
         [chen] => chenling 
         [ling] => chenli 
       ) 
 
     ) 
 
     [sex] => 男 
     [age] => 25 
   ) 
 
   [1] => stdClass Object 
   ( 
     [name] => sun 
     [sex] => 女 
     [age] => 24 
   ) 
 
  ) 
 
)*/

比如说我要取得了值是chenling的那个元素,则应该这样访问:

$J->item1[0]->name[0]->chen;//这将取得元素chen的值:chenling

其实这种访问形式结合了对象和数组的访问方式,也相当于访问一个5维数组.

形式3:完全数组形式,代码如下:

$json=&#39;[["item1","item11"],["n","chenling"],["m","llll"]]&#39;; 
$J=json_decode($json); 
print_r($J);  
/*将输出: 
Array 
( 
  [0] => Array 
  ( 
    [0] => item1 
    [1] => item11 
  ) 
 
  [1] => Array 
  ( 
    [0] => n 
    [1] => chenling 
  ) 
 
  [2] => Array 
  ( 
    [0] => m 
    [1] => llll 
  ) 
)*/

比如说我要取得了值是chenling的那个元素,则应该这样访问:

$J[0][1];//这将取得元素值chenling的那个元素

但是用这种方式有一个缺点,就是无法用字符串作为索引,只能用数字,用完全对象的形式可以解决这个问题.其实这种访问形式就是数组的访问方式,相当于访问一个2维数组.

小结:从上面的例子可以看出JSON有点类似XML,也可以在PHP和Javascript之间传递带结构的数据,使用起来很方便.


文章链接:

随便收藏,请保留本文地址!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.