读书人

js处置json

发布时间: 2013-10-22 16:17:03 作者: rapoo

js处理json
1:生成下接列表

<html><head><script>var arrayNation = [{"id":0,"devName":"billing"},{"id":1,"devName":"log"}];function createSelect(str){var _select = document.createElement("select");var firstOption = document.createElement("option");firstOption.value = "------select------";firstOption.text = "------请选择------";_select.appendChild(firstOption);for ( i = 0; i < str.length; i++){var _option = document.createElement("option");_option.value = str[i].id.toString();_option.text = str[i].devName.toString();_select.appendChild(_option);}return _select;}</script></head><body><div id="dep"/><script>document.getElementById("dep").appendChild(createSelect(arrayNation));</script></body></html>
?2:区分对像与数组
JSON对象:
var str2 = { "name": "cxh", "sex": "man" };?3JSON字符串转换为JSON对象
_option.selected="selected"
?

读书人网 >JavaScript

热点推荐