A method for dynamically reading JSON to parse key value pairs

  • 2020-03-30 03:11:45
  • OfStack

Json format: {result: [{" aaa ", "111"}, {" ABC ", "121"}, {" cba ", "212"})}
 
function $$(id){return document.getElementById(id);} 

 
function changelayer(id,oid){ 

$$(id).length=1; 


$.ajax({ 
type: "POST",//Request way
url: "getOption.jsp",//The address is the action request path
data: "json",//The data type text XML json script jsonp
success: function(msg){//The returned parameter is the processed returned data in the action

var dataObj=eval("("+msg+")"); //You don't need to convert to json here if the action is specified as json

//Add all to the drop-down list

$.each(dataObj.result, function(key,value){ 

for (var one in value){ 
$$(id).options.add(new Option(value[one],one));//The value [one] value one
} 

}); 

} 
}); 

} 

Related articles: