Js How to traverse all key of JSON object and get value from dynamic key

Source: Internet
Author: User

JS Access JSON Reference code

"Content-type"Content="text/html; CHARSET=GBK"/> <script type="Text/javascript">varSmstypedesc = {"4":"Return SMS","3":"Email SMS","AA":"Test SMS"}; function Enumakey () { for(varKeyinchSmstypedesc)              {alert (key); }} function Getval () {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]);          } function Getvalbykey () {alert (SMSTYPEDESC.AA); }      </script> JSON object:<br/> <pre>varSmstypedesc = {"4":"Return SMS","3":"Email SMS","AA":"Test SMS"}; </pre> <input type="Button"onclick="Enumakey ();"Value="traverse Smstypedesc All keys"/> <input type="Button"onclick="getval ();"Value="gets the value of the SMSTYPEDESC dynamic specified key"/> <input type="Button"onclick="Getvalbykey ();"Value="gets the value corresponding to the SMSTYPEDESC key AA"/> </body>

The above code can see that the value of JS access JSON is mainly through the "key" "value" way to access,

Excerpted from http://blog.csdn.net/love__coder/article/details/7532616

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.