什么是json,您能向新手解释吗?

编程入门 行业动态 更新时间:2024-10-11 03:20:28
本文介绍了什么是json,您能向新手解释吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您能用最简单的词来解释吗?

Can you explain it in most simple words?

最好的演示脚本.

推荐答案

JSON是一种共享数据的方式(通常在浏览器和服务器之间).

JSON is a way of sharing data (usually between the browser and a server).

JavaScript提供了两种存储值集合的方式:

JavaScript allows for two way to store collections of values:

//arrays: [value, value, value] //objects: {key:value, key:value, key:value}

在某个时候,一个名叫Doug的专家意识到,将数据发送到已经像对象一样已经建立的JavaScript上通常是最有效的. [而不是PHP发送逗号分隔的字符串,后数据,XML甚至HTML,所有这些都必须由JavaScript精心解析.]

At some point, a guru known as Doug realized that it is usually most efficient to send data to JavaScript already setup like an object. [Rather than PHP sending a comma-delimited strings, post-data, XML, or even HTML, all of which have to be painstakingly parsed by the JavaScript].

因此他将这个想法称为JSON,并为其编写了规范,然后标准就诞生了.

So he called that idea JSON, wrote up a spec for it, and the standard was born.

例如,假设您的login.php脚本应返回用户名,帖子总数和自注册以来的天数:

For example, let's say your login.php script should return the users name, total posts, and days since registered:

//XML "<xml..><details>\ <user>Jim</user><posts>239</posts><since>Jan09</since>\ </details>" //POSTData "user=Jim&posts=239&since=Jan09" //JSON "{user:'Jim', posts:239, since:'Jan09'}"

最后一个可以由JS轻松解析(使用eval),并且可以直观地使用细节:

The last one can be easily parsed by JS (using eval), and the details can be used intuitively:

var user = details.user;

正确地指出,要使用有效的JSON,所有字符串都必须用双引号引起来. 这样做是为了防止JS破坏保留关键字(在JS中,除非使用引号,否则可能不使用某些词,例如class.因此,不能使用{class:'mike'}.)

It was correctly noted that to be valid JSON, all strings must be double quoted. This was done to prevent JS from croaking on reserved keywords (in JS one may not use certain words, such as class, unless they are quoted. So {class:'mike'} cannot be used).

还应该指出,PHP 5.2+具有可用于创建或解析JSON的功能:

It should also be pointed out that PHP 5.2+ has functions which can be used to create or parse JSON:

<?php $arr = array ('a'=>'ay','b'=>'bee','c'=>'cee'); echo json_encode($arr); //outputs {"a":"ay","b":"bee","c":"cee"} ?>

json_decode函数仅在JSON有效的情况下才起作用,因此正确使用双引号有时很重要.

The json_decode function will ONLY work if the JSON is valid, so it is sometimes important to get those double-quotes right.

更多推荐

什么是json,您能向新手解释吗?

本文发布于:2023-11-10 07:11:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574702.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:您能   新手   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!