For JSON pairs, how to traverse all the keys of a JSON object, when using a JSON object, if the key is not known, how to get the value by the key variable

Source: Internet
Author: User

For JSON pairs, how do you traverse all the keys of a JSON object, and when using a JSON object, if you can't know the key, how do I get the value through the key variable? See the following key code:

<HTML><Head>    <Metahttp-equiv= "Content-type"content= "text/html; CHARSET=GBK"/>    <Scripttype= "Text/javascript">        varSmstypedesc= {"4":"Return SMS","3":"Email SMS","AA":"Test SMS"}; functionEnumakey () { for(varKeyinchSmstypedesc)            {alert (key); }        }        functionGetval () {varKey=Prompt ("Please enter the key to query","4"); if("undefined"==typeof(SMSTYPEDESC))return; if("undefined"==typeof(Smstypedesc[key])) {Alert ("input key:"+Key+", it does not exist in the JSON object! "); return; } alert ("The key you entered is:"+Key+ ", the value corresponding to this key is:"+Smstypedesc[key]); }        functionGetvalbykey () {alert (SMSTYPEDESC.AA); }    </Script></Head><Body>JSON object:<BR/>    <Pre>var Smstypedesc = {"4": "Return Message", "3": "Message sms", "AA": "Test SMS"}; </Pre>    <inputtype= "button"onclick= "Enumakey ();"value= "Traverse smstypedesc all Keys"/>    <inputtype= "button"onclick= "getval ();"value= "Get the value of SMSTYPEDESC dynamically specified key"/>    <inputtype= "button"onclick= "Getvalbykey ();"value= "Get the value corresponding to the SMSTYPEDESC key AA"/></Body></HTML>

It's easy to see through the code above. Depending on the dynamic key, it is accessed primarily through [], for the passage of the known key. Access

For JSON pairs, how to traverse all the keys of a JSON object, when using a JSON object, if the key is not known, how to get the value by the key variable

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.