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