Home  >  Article  >  Web Front-end  >  How to get the attributes and values ​​of json object?

How to get the attributes and values ​​of json object?

青灯夜游
青灯夜游Original
2019-05-31 15:29:3117730browse

JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy for humans to read and write. It is also easy for machines to parse and generate. It is based on a subset of JavaScript (Standard ECMA-262 3rd Edition - December 1999). JSON uses a completely language-independent text format, but also uses conventions similar to the C language family (including C, C, C#, Java, JavaScript, Perl, Python, etc.). These properties make JSON an ideal data exchange language.

How to get the attributes and values ​​of json object?

The following describes how to obtain JSON attributes and values:

Method 1: Use for in to traverse

1. Use for in to traverse to obtain attributes

var myObj = { "name":"runoob", "alexa":10000, "site":null };
for (x in myObj) { console.log(x); }

Output:

How to get the attributes and values ​​of json object?

2. Use for in to traverse to obtain Attribute value

var myObj = { "name":"runoob", "alexa":10000, "site":null };
for (x in myObj) {
console.log(myObj[x]);
}

Output:

How to get the attributes and values ​​of json object?

Note: When using for traversal, the value of the corresponding attribute can only be obtained through myObj[x] , instead of using myObj.x

Method 2: Use the dot (.), which is the form of object name.property name to access

var myObj = { "name":"runoob", "alexa":10000, "site":null };
console.log(myObj.name);

Output:

How to get the attributes and values ​​of json object?

Method 3: Use square brackets ([]), that is, dictionary indexing to access

var myObj = { "name":"runoob", "alexa":10000, "site":null };
console.log(myObj['name']);  // 输出的是 name 值

Output:

How to get the attributes and values ​​of json object?

The above is the detailed content of How to get the attributes and values ​​of json object?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:What does eval mean in jsNext article:What does eval mean in js