什么是 JSON 文本?
Javascript Object Notation (JSON) 用于在 Internet 上传输或交换信息。 JSON 只是作为 javascript 对象编写的纯文本。存在几个代表一些有用信息的键值对。
需要记住的重要一点是,尽管 JavaScript 以 JSON 的完整形式出现,但它不仅限于 JavaScript 编程语言,它只是一些信息的表示,可以在任何编程语言、库或任何地方使用框架。一些编程环境具有解析和生成 JSON 数据的功能。
JSON的一些属性:
- 它易于编写,紧凑,自我描述。
- 它既是机器可以理解的,也是人类可以理解的。
- 它纯粹是一个字符串。
- 它可以是数字、字符串、值、对象或数组,但最常用的是最后两个。
- 与 javascript 对象不同,JSON 中的键不能包含方法,只允许包含属性。
- 只有双引号用于将字符串或属性包裹在 JSON 中。
- 我们可以通过编写 .json 扩展名来创建 JSON 文件。
- 媒体类型是应用程序/json。
JSON的用例:
- 在 Web 应用程序中将数据从服务器传输到客户端,反之亦然。
- 通过 Postman 等任何软件测试 REST API。
- 在两个或多个不同的编程环境之间交换数据。
- 它用于在网络上传输和接收结构化数据。
例子
Object JSON:这是一个包含极客信息的简单对象。
Javascript
{
"name": "GeeksforGeeks",
"about": "A portal for Computer Science Geeks to read the articles related",
"additionalDescription": "It also provides some Courses",
"founder": "Sandeep Jain",
"mail": "feedback@geeksforgeeks.org",
"address": "Sector-136, Noida, Uttar Pradesh - 201305"
}
Javascript
[
{
"name": "Data Structures with C++ Live",
"description": "A course specially designed for C++ enthusiasts",
"whatYouWillLearn": [
"Mastering DS from basic to advanced level",
"Solving problems which are asked in product-based companies",
"How to become a strong and efficient problem solver",
"Enhance your problem-solving ability"
]
},
{
"name": "JAVA Backend Development - Live",
"description": "Learn backend development with Hibernate, RESTful APIs",
"whatYouWillLearn": [
"Redis & Kafka with Spring Boot",
"Advanced Java",
"Spring / Spring Boot",
"Micro-services & related technologies used to build Java-based web applications"
]
},
{
"name": "System Design - Live",
"description": "LIVE System Design classes for individuals looking to crack SDE job",
"whatYouWillLearn": [
"How to design scalable systems",
"Tips to crack system design interviews",
"The intensity of the interview & how to manage your time",
"Real-world example questions like designing system of Uber, Twitter etc.",
"How to apply theory into application in real-time"
]
}
]
Javascript
Array JSON:这是一个简单的 JSON 数组,包含极客的极客课程列表。
Javascript
[
{
"name": "Data Structures with C++ Live",
"description": "A course specially designed for C++ enthusiasts",
"whatYouWillLearn": [
"Mastering DS from basic to advanced level",
"Solving problems which are asked in product-based companies",
"How to become a strong and efficient problem solver",
"Enhance your problem-solving ability"
]
},
{
"name": "JAVA Backend Development - Live",
"description": "Learn backend development with Hibernate, RESTful APIs",
"whatYouWillLearn": [
"Redis & Kafka with Spring Boot",
"Advanced Java",
"Spring / Spring Boot",
"Micro-services & related technologies used to build Java-based web applications"
]
},
{
"name": "System Design - Live",
"description": "LIVE System Design classes for individuals looking to crack SDE job",
"whatYouWillLearn": [
"How to design scalable systems",
"Tips to crack system design interviews",
"The intensity of the interview & how to manage your time",
"Real-world example questions like designing system of Uber, Twitter etc.",
"How to apply theory into application in real-time"
]
}
]
Javascript 中的JSON:在 javascript 中,我们可以简单地使用 javascript 的内置stringify() 方法创建 JSON。
Javascript
输出:-当我们在浏览器中打开包含上述代码片段的 index.html 文件时,这将在控制台中发生。