为什么需要使用JSON在PHP和AJAX [英] Why need to use JSON in php and AJAX

查看:123
本文介绍了为什么需要使用JSON在PHP和AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始做的jQuery上周,到目前为止,我已经做了一些基本制度与阿贾克斯,如基本的jQuery的CRUD和简单的聊天系统,而不引用对其他的工作,我决定测试自己多远我可以单独完成系统jQuery中(不包括JSON和XML还)。

I just started doing jQuery last week, and so far I already made some basic systems with ajax, like basic jQuery CRUD and simple chat system without referencing on other's work for I decided to test myself on how far I can do systems alone in jQuery(without JSON and XML yet).

但是,当我决定看看其他人的工作(希望能得到/学习好的做法和codeS在那里)多或几乎每一个使用Ajax处理程序有一定的JSON在里面。所以,我决定学习和阅读JSON 专门这一的,但我想,因为这是我第一次与它打交道,我中号有一个问题,下沉到我的大脑。是的,我知道这是一个描述分层数据的轻量级的方式,我也知道如何让JSON等混合在JS文字数组和对象,以及如何dsplay它JS。

But when I decided to look at other's work (hoping to get/learn good practices and codes out there) many or almost every program that deals with ajax have some JSON in it. So I decided to study and read JSON specially this one, but I guess because it's my first time dealing with it, I'm having a problem sinking it into my brain. Yeah I know it is a "lightweight way of describing hierarchical data", I also know how to make JSON like mixing a literal array and object in JS, and how to dsplay it in js.

不过,我的问题是,有什么区别,什么是优势比不使用它? 当我仍然可以使用Ajax和数据库,而JSON在服务器上存储数据。 顺便说一句我还没有专注于XML还因为根据从我的研究,最好使用JSON的AJAX。

But my question is, what's the difference and what's the advantage than not using it? When I can still get and store data on the server using ajax and database without JSON. By the way I haven't focus on XML yet because based from my research it's better to use JSON in AJAX.

您可以给我处理一些实际情况,

Can you give me some actual scenario dealing with

S1。阿贾克斯PHP的MySQL(这个什么缺点?的)

s1. ajax php mysql (this with what disadvantages?)

S2。阿贾克斯PHP的MySQL JSON(这与什么好处呢?的)

s2. ajax php mysql json (this with what advantages?)

我的意思是,我的重点是发送和获取数据,我已经可以与S1做到这一点。

I mean, my focus is to send and get data, and I already can do it with s1.

很抱歉,如果你发现我的问题愚蠢的。蒂亚。 :)

Sorry if you find my question stupid. Tia. :)

推荐答案

为什么要使用JSON?答案是便携结构

Why use JSON? The answer is portability and structure.

JSON是移动,因为解析器和作家可用于许多,许多语言。这意味着,JSON,一个PHP脚本生成可以很容易地通过一个JavaScript脚本的理解。它是传输复杂的结构像阵列和对象,并把它仍然是多语言兼容的最佳方式。

JSON is portable because parsers and writers are available for many, many languages. This means that JSON that a PHP script generates can be very easily understood by a JavaScript script. It is the best way to transmit complex structures like arrays and objects, and have it still be compatible with multiple languages.

JSON提供的结构,因为你传送它的数据能够具有一致的格式。这是不是发送回纯文本(即未格式化)数据,如逗号分隔或分隔的数据。

JSON provides structure because the data you transmit with it can have consistent formatting. This is instead of transmitting back plain-text (i.e. unformatted) data, like comma-separated or delimited data.

数据是仅分隔(例如,BookName1,BookName2,BookName3)是比较困难的人类理解,调试,并与工作。如果你想调试你的服务器和你的浏览器,并划定(如上面的示例所示)的数据之间的响应,你可能很难理解它。另外,如果你想添加不同的数据类型,提供不同的记录等,那么你的自定义数据格式变得更加复杂。最终,你可能最终重塑JSON。

Data that is merely delimited (for example, "BookName1,BookName2,BookName3") is more difficult for humans to understand, debug, and work with. If you wanted to debug a response between your server and your browser and the data was delimited (like my example above), you might have a hard time understanding it. Also, if you want to add different data types, provide separate records, etc., then your custom data format becomes more complicated. Eventually, you might end up reinventing JSON.

作为一个方面说明,JSON的确比XML更好。这是更有效的空间明智。没有标签名称占用空间。结构是通过嵌套括号创造,而不是冗长的标签。

As a side note, JSON is indeed better than XML. It is much more efficient space-wise. There are no tag names to take up space. Structure is created via nested braces, instead of verbose tags.

资源

下面是XML和JSON的差异和优点/缺点一篇有趣的文章: http://www.json.org/ xml.html

Here is an interesting article on the differences and pros/cons of XML and JSON: http://www.json.org/xml.html

例子

根据您的要求,这里是用PHP编码JSON的例子。

Per your request, here is an example of encoding JSON with PHP. This is ripped from the docs:

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

echo json_encode($arr);

输出:

{"a":1,"b":2,"c":3,"d":4,"e":5}

与此不同,这样的事情,没有JSON:

Contrast this to something like this, without JSON:

a,1
b,2
c,3
d,4
e,5

要分析的是,你不得不遍历每个行,自己分裂值,然后创建阵列。这并不难,但是想象一下,你有一个嵌套的对象:

To parse that, you'd have to iterate through each line, split the values yourself, and then create the array. This isn't that difficult, but imagine you have a nested object:

$arr = array ('a'=> array(1,2,3),'b'=> array('a' => 1, 'b' => 2),'c'=>3,'d'=> array(1,2,3,4,5) ,'e'=>5); // etc.

使用JSON,这是没有什么不同的连接code吧。只需使用 json_en code 。但是,手动编码本,然后对其进行解码手动将显著更多的工作。

With JSON, it's no different to encode it. Just use json_encode. But, encoding this manually, and then decoding it manually would be significantly more work.

这篇关于为什么需要使用JSON在PHP和AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆