Home Web Front-end JS Tutorial JSON Learning JSON in JavaScript detailed instructions_json

JSON Learning JSON in JavaScript detailed instructions_json

May 16, 2016 pm 06:34 PM
json study

Copy code The code is as follows:




1. Create an object using JSON in javascript
Js code
Copy code The code is as follows:

//Create an empty object
var JSONObject = {}
//Create a new object
var JSONObject = new Object()
/ /Create an object containing properties, where the name is a string and the age is an integer
var JSONObject = {
"name":"kevin",
"age":23
}

Similar to java, we can get the properties of an object through the dot (.) operator.
Copy code The code is as follows:

var JSONObject = {
"name":" kevin",
"age":24,
};

alert("JSONObject.name:" JSONObject.name);
alert("JSONObject.age:" JSONObject.age );

2. Use JSON to create array objects in javascript

Create a Student object, which contains two array objects, each array object , contains the properties of the Student object.

Copy code The code is as follows:

var student = {
// The first array object Class
"Class":[
"{
"name":"kevin",
"className":"java",
"age":23
                                                                                 
],
/ /The second array object
"Score":[
" {
" name": "shower",
"score":100
},
{
" name":"zheng",
"score":100
" " }
" " ]
}

var i=0;

for(i=0; i alert("student.Class[" i "].name===>" student.Class[i].name);
alert("student .Class[" i "].className===>" student.Class[i].className);
alert("student.Class[" i "].age===>" student.Class [i].age);
}

for(i=0;i alert("student.Score[" i "].name ===>" student.Score[i].name);
alert("student.Score[" i "].score===>" student.Score[i].score);
}



3. Use JSON to create messages in javascript



Copy code
The code is as follows:

//create a Student Object
var Student = {
    "Math":[{
            "name":"kevin",
            "mark":70,
            "age":23
        },{
            "name":"smart",
            "mark":40,
            "age":25
        }
    ],
    "Science":[{
            "name":"kevin2",
            "mark":70,
            "age":23
        },{
            "name":"smart2",
            "mark":40,
            "age":25
        }
    ]
}

//print array value
var i = 0;
var array = new Array();

for(i=0;i    array.push(Student.Math[i].name);
    array.push(Student.Math[i].mark);
    array.push(Student.Math[i].age);
}

for(i=0;i    array.push(Student.Science[i].name);
    array.push(Student.Science[i].mark);
    array.push(Student.Science[i].age);
}
alert("array==>" array);

//This method produce a JSON text from a JavaScript value.
//这个方法将一个JavaScript值转换为一个JSON字符串
alert("array.toJSONString()==>" array.toJSONString());
alert("String.parseJSON==>" array.toJSONString().parseJSON());
var data2 = array.toJSONString().parseJSON();
if(data2 instanceof Array){
    alert("Array");
}

复制代码 代码如下:

//表达式有浏览器兼容问题
//var cx = /[u0000u00adu0600-u0604u070fu17b4u17b5u200c-u200fu2028-u202fu2060-u206fufeffufff0-uffff]/g,
// escapable = /[\"x00-x1fx7f-x9fu00adu0600-u0604u070fu17b4u17b5u200c-u200fu2028-u202fu2060-u206fufeffufff0-uffff]/g,
//这个是修正后的
var cx = new RegExp('/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g'),
escapable = new RegExp('/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g'),
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

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

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

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

Learn the main function in Go language from scratch Learn the main function in Go language from scratch Mar 27, 2024 pm 05:03 PM

Title: Learn the main function in Go language from scratch. As a simple and efficient programming language, Go language is favored by developers. In the Go language, the main function is an entry function, and every Go program must contain the main function as the entry point of the program. This article will introduce how to learn the main function in Go language from scratch and provide specific code examples. 1. First, we need to install the Go language development environment. You can go to the official website (https://golang.org

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.

PHP Tutorial: How to Convert JSON Unicode to Chinese Characters PHP Tutorial: How to Convert JSON Unicode to Chinese Characters Mar 05, 2024 pm 06:36 PM

JSON (JavaScriptObjectNotation) is a lightweight data exchange format commonly used for data exchange between web applications. When processing JSON data, we often encounter Unicode-encoded Chinese characters (such as "u4e2du6587") and need to convert them into readable Chinese characters. In PHP, we can achieve this conversion through some simple methods. Next, we will detail how to convert JSONUnico

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.

See all articles