JSON进阶第一篇 在PHP与javascript 中使用JSON
一.JSON简介
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。JSON的详细解释请访问JSON官网,这个网站上有图有真相,建议认真学习下。另外,JSON在维基百科上的介绍也很详细,可以访问一下。
二.PHP中JSON编码
在PHP中生成JSON字符串是非常容易的,直接使用json_encode()函数就可以将PHP数据转换成JSON字符串,此函数原形如下:
string json_encode ( mixed $value )
这个函数可以为任何数据进行转码,除了resource类型。
三.Javascript 解析JSON
有二种方法:一种是直接使用eval()函数。这种方法最快速。然而由于eval方法同样可以执行任意的JavaScript代码,因此当数据来源不可靠时则可能产生安全性问题。比如下面这个例子就会导致页面被重定向:
[html] view plaincopy
一段使用eval()解释会导致页面被重定向的JSON数据
第二种方法可以防止不安全代码出现——通过浏览器原生支持的JSON.parse(str)方法读取JSON数据, 该方法采用解析器验证读入的代码是否真的是JSON代码,这样就提供了较好的安全性。但是,由于这是用模拟的方式读取,速度上会比eval()慢。
四.JSON实例
下面以一个实例来解释数据是如何被编码成JSON字符串,JSON字符串又是如何在javascript中解析使用的,程序分为json1.php及json1.html。程序还要引用Smarty、JQuery及JSON库文件。
1.json1.php
[php] view plaincopy
// by MoreWindows( http://blog.csdn.net/MoreWindows )
require_once ('../../smarty_libs/Smarty.class.php');
$tpl_article_array = array(
"001" => array(
"title"=>"PHP访问MySql数据库 初级篇",
"link"=>"http://www.BkJia.com/kf/201112/115227.html"
),
"002" => array(
"title"=>"PHP访问MySql数据库 中级篇Smarty技术",
"link"=>"http://www.BkJia.com/kf/201112/115229.html"
),
"003" => array(
"title"=>"PHP访问MySql数据库 高级篇AJAX技术",
"link"=>"http://www.BkJia.com/kf/201112/115230.html"
),
);
$tpl_article_json = json_encode($tpl_article_array);
$tpl = new Smarty();
$tpl->assign("article_array", $tpl_article_array);
$tpl->assign("article_json", $tpl_article_json);
$tpl->display("json1.html");
?>
2.json1.html
www.2cto.com
[html] view plaincopy
{foreach $article_array as $key=>$value}
{$value['title']}
{/foreach}
运行结果如下(Win7+IE9.0):
当鼠标经过三个标题时,会触发mouseenter事件显示提示语句。
下一篇《JSON进阶第二篇AJAX方式传递JSON数据》将介绍如何用AJAX动态请求得到JSON数据并生成标题及提示语句。
转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/7197971

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
