Home Web Front-end JS Tutorial Objects and JSON in JavaScript_Basics

Objects and JSON in JavaScript_Basics

May 16, 2016 pm 03:51 PM
javascript json object

Introduction

JSON is JavaScript Object Natation. It is a lightweight data exchange format that is very suitable for the interaction between the server and JavaScript.
JSON is a data exchange format, like XML and YAML, a way to transfer structured information between various languages. On the other hand, JavaScript objects are a data type in the JavaScript language, just like arrays in PHP, classes and structures in C.

Define JSON and javascript objects

When defining an object in a JavaScript program, the attribute name of the object can be enclosed in double quotes or not. If the attribute name contains special characters (such as!, if, etc.), double quotes must be added.
When defining JSON, the attribute name must be enclosed in double quotes.

Code example:

1. Define javascript object

Copy code The code is as follows:

var obj={name:"tudouya","sex":"man"}; #Two attributes can be added with double quotes or without
var obj={"!":"hello world"}; #Double quotes must be added when the attribute name contains special characters

2. Define JSON string
Copy code The code is as follows:

var jsonString={"name":"tudouya"}; #Double quotes must be added when defining JSON

javascript object converted to JSON

1. Convert javascript object to JSON

We can use javascript’s built-in function to convert javascript objects to JSON. This function is JSON.stringify().
Code example:

Copy code The code is as follows:

var obj={name:"tudouya",sex:"man"};
var jsonObj=JSON.stringify(obj);
console.log(jsonObj);
##The output result is: {"name":"tudouya","sex":"man"}

When converting JavaScript objects to JSON, there is one thing we need to pay attention to:
If the object contains attributes whose values ​​are functions and dates, JSON ignores the attributes whose values ​​are functions and converts the attributes whose values ​​are dates into strings.
Code example:
Copy code The code is as follows:

var obj={
name:"tudouya",
birthday:new Date(),
action:function (){
document.write("walk");
}
};
var jsonObj=JSON.stringify(obj);
console.log(jsonObj);
##The output result is: {"name":"tudouya","birthday":"2014-08-12T10:05:00.497Z"}

Parsing JSON in javascript

In older versions of JS, everyone usually uses the eval() function to parse JSON, but ECMAScript5 provides us with a new function JSON.parse() for parsing JSON.

The use of this function is relatively simple, you can try it yourself. When this function is applied to a JSON string, the JSON is converted into a JavaScript object. That is to say, when the typeof operator is used to view the type of the function, the returned value is Object.
Another thing to note is that this function is only supported after ECMAScript 5. If it is an older version of the browser, it may not support this function. The solution is to load a js file that implements this function, namely json2.js. If you are using the JQuery framework, jQuery.parseJSON(), this function calls the JSON.parse() method.
Regarding using the eval() method to parse JSON, this will be recorded after in-depth study.

A very important concept

As a front-end rookie, I often hear people say "JSON object", but in fact there is no concept of "JSON object". The real form of JSON is a string.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese Mar 05, 2024 pm 02:48 PM

In-depth understanding of PHP: Implementation method of converting JSONUnicode to Chinese During development, we often encounter situations where we need to process JSON data, and Unicode encoding in JSON will cause us some problems in some scenarios, especially when Unicode needs to be converted When encoding is converted to Chinese characters. In PHP, there are some methods that can help us achieve this conversion process. A common method will be introduced below and specific code examples will be provided. First, let us first understand the Un in JSON

How do PHP functions return objects? How do PHP functions return objects? Apr 10, 2024 pm 03:18 PM

PHP functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.

What should I pay attention to when a C++ function returns an object? What should I pay attention to when a C++ function returns an object? Apr 19, 2024 pm 12:15 PM

In C++, there are three points to note when a function returns an object: The life cycle of the object is managed by the caller to prevent memory leaks. Avoid dangling pointers and ensure the object remains valid after the function returns by dynamically allocating memory or returning the object itself. The compiler may optimize copy generation of the returned object to improve performance, but if the object is passed by value semantics, no copy generation is required.

How to use PHP functions to process JSON data? How to use PHP functions to process JSON data? May 04, 2024 pm 03:21 PM

PHP provides the following functions to process JSON data: Parse JSON data: Use json_decode() to convert a JSON string into a PHP array. Create JSON data: Use json_encode() to convert a PHP array or object into a JSON string. Get specific values ​​of JSON data: Use PHP array functions to access specific values, such as key-value pairs or array elements.

Quick tips for converting PHP arrays to JSON Quick tips for converting PHP arrays to JSON May 03, 2024 pm 06:33 PM

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

See all articles