Two ways JavaScript gets the key name of a JSON object

Source: Internet
Author: User

JavaScript gets the JSON object's key name in two ways of data processing, you may receive a JSON object in an indeterminate content format, and then extract the value of the key. Today, we tried two methods to extract the JSON key, all of which worked correctly. Let's look at the first method.
jsonobj = {Name: ' Richard ', Value: ' 8 '} 
for (key in jsonobj) {
Console.log (key);
Add your statement to get key value
}

result

Name
Value
The second method of

In JavaScript, object has a key property that can return an array of key of the JSON object

(Object has a property keys, returns an Array of keys from that Object)

Usage:

object.keys (jsonobj)  
jsonobj = {Name: ' Richard ', Value: ' 8 '} 
Console.log (Object.keys (jsonobj));

result

[ ‘Name‘, ‘Value‘ ]
The second method is relatively convenient to obtain the results.

Two ways JavaScript gets the key name of a JSON object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.