js解析json对象获取属性名及值(key value值)
不知道json串属性名字的时候,需要获取属性名。直接附代码:
//基本json串 var jsonObj = {"55":"1","70":"0","80":"2","60":"2"}; for (var prop in jsonObj) { alert("jsonObj[" + prop + "]=" + jsonObj[prop]); } var jsons = [{"fmergername":"中国,山东省,济南市","fid":"370100","fname":"济南市","fparentid":"370000","fshortname":null,"flevel":2,"fcitycode":null,"fzipcode":null,"flng":null,"flat":null,"fspell":null}]; var len = jsons.length; //动态遍历json数组 for (var i = 0; i < len; i++) { for (var prop in jsons[i]) { alert("jsonObj[" + prop + "]=" + jsons[i][prop]); } } var json= { "Type": "Coding", "Height":100 }; for (var key in json) { alert(key); //Type, Height alert(json[key]); //Coding, 100 } $.each(json, function(i) { alert(json[i]); //Coding, 100 alert(i); //Type, Height }); 注意:当json是js对象时,直接遍历的出的key和value的值,但是当json为数组时遍历出的是1,2,3,4……和object
本文原创,转载必追究版权。