JSON and JavaScript Introduction and Differences

Continue to create, accelerate growth! This is the 5th day of my participation in the "Nuggets Daily New Plan · June Update Challenge", click to view the details of the event

Web development relies on JavaScript, but what is JSON and how is JSON different from JavaScript? Understanding the technology behind the WEB is important for teams looking to innovate and make the most of available resources. Everything related to the web will use JavaScript. However, JSON is more nuanced and you may or may not need to use it.

JSON and JavaScript

Before you start comparing JSON and JavaScript, you need to understand what JSON is and how it relates to JavaScript. Let's review what JavaScript and JSON are, then compare and contrast them to see what the differences are.

What is JavaScript?

Along with HTML and CSS, JavaScript is one of the core technologies of the World Wide Web. JavaScript started out to bring web pages to life. Without it, modern web design would be static and boring. JavaScript is a programming language. It's probably the most important programming language because every aspect of web development is built on it, and it's going to get more and more weight in the future. This article won't spend a lot of time detailing what JavaScript is.

what is JSON

JSON JavaScript Object Notationis short for . Is the same for JSON JavaScript? not quite. JSON is a data format independent of any programming language, derived from JavaScript. Most modern programming languages ​​include methods that can generate and parse JSON data.

A few notes about JSON:

  • It is a lightweight format for storing and transferring data
  • The grammar is self-describing and easy for humans to read and understand
  • Typically, JSON is used when data is sent from a server to a web page, as the API's transport data format

JSON 数据以两种基本方式构造:key/value 对或此类对的集合,或有序列表。JSON 允许存储或传输六种不同类型的数据,包括:

  • Array
  • Null
  • Boolean
  • String
  • Number
  • Object

如上所述,JavaScript 和 JSON 是两个完全不同的东西。虽然 JSON 是从 JavaScript 派生的,但两者在很多方面没有可比性。但是,可以将 JSON 与 JavaScript 对象进行比较,这两者关系更密切,在前端开发中也是经常接触的。

什么是 JavaScript 对象?

JavaScript 对象也用于存储数据,所有 JavaScript 值实际上都是对象,除了原始数据类型 nullBooleannumberstringsymbolundefined 。JavaScript 对象可能会变得复杂,因为它们可以包含许多不同的原始数据组合。

JavaScript 对象也以 key/value 对的形式构成,它们可以通过对象构造器语法、对象字面量、构造器和原型链创建, JavaScript 对象是可变的。这意味着可以更改 key/value 对中各个键的值。

JSON 对比JavaScript 对象

在了解 JSON 和 JavaScript 对象的不同之处之前,重要的是要了解 JSON 和 JavaScript 对象的相似之处远大于它们的不同之处。JSON 源自 JavaScript 对象文字语法。事实上,最简单的解释是 JSON 是 JavaScript 对象文字语法,但有更多限制。

JSON 和 JavaScript 对象都是人类可读的。它们都为用户提供了一种结构化数据的方法,并且它们都可以用作另一个来源的来源。当谈到差异时,JSON 和 JavaScript 对象在几个关键方面有所不同:

  • 语言依赖
  • 纯文本
  • String Vs Object

语言依赖

One of the biggest differences between JSON and JavaScript objects is the programming language dependency. JavaScript objects are completely dependent on JavaScript, and they cannot be used with any other programming language. On the other hand, more than 50 different programming languages ​​support JSON, including popular ones such as:

  • Go
  • Ruby
  • Perl
  • C
  • C#
  • Python
  • PHP
  • JavaScript

This broad support makes JSON a flexible choice for data storage and transmission.

Plain Text

JSON data can only be presented as text, and no comments or other lines of code can be added to JSON, which is why so many other programming languages ​​can generate and parse JSON. JavaScript objects can contain other code, such as functions and methods.

String Vs Object

The last major difference between JSON and JavaScript objects is how they are rendered. JSON is rendered as a string. These are called JSON strings. JavaScript objects can contain strings, but as the name suggests, they are objects and not strings. Objects are more complex than strings.

Summarize

The difference between JSON and JavaScript objects is subtle, and they are also two types that are often used in front-end development. It is especially important to note that JSON does not have the problem of deep copy and shallow copy, and objects need to consider this problem, so in order to avoid Problems caused by deep copying of objects, usually converting objects to and from JSON.

Guess you like

Origin juejin.im/post/7105599849110143013