⑴ js 怎么遍历json对象所有key及根据动态key获取值
json是种常用数据传输格式,js怎么解析json格式的数据呢,请参阅下面的代码:
[html] view plain print?
<html>
<head>
<scripttype="text/javascript">
vardata='{"return_code":0,"return_message":"success","data":{"data":[{"id":"1","question":"公主令牌在哪交?"},{"id":"2","question":"公主护使有什么用?"},{"id":"3","question":"角斗场在哪?"},{"id":"4","question":"北部断层在哪?"},{"id":"5","question":"欢乐令有什么用?"},{"id":"6","question":"令牌积分有什么用?"},{"id":"7","question":"南部断层在哪?"},{"id":"8","question":"大妖魔令牌交给谁?"},{"id":"9","question":"神工坊在哪?"},{"id":"10","question":"警戒妖珠有什么用?"}]}}';
functionShowData(){
varobj=eval("("+data+")");
alert("return_code:"+obj["return_code"]);
alert("return_message:"+obj["return_message"]);
alert("第一个问题id:"+obj["data"]["data"][0]["id"]);
alert("第一个内容id:"+obj["data"]["data"][0]["question"]);
}
</script>
</head>
<bodyonload="ShowData();">
{"return_code":0,"return_message":"success","data":{"data":[{"id":"1","question":"公主令牌在哪交?"},{"id":"2","question":"公主护使有什么用?"},{"id":"3","question":"角斗场在哪?"},{"id":"4","question":"北部断层在哪?"},{"id":"5","question":"欢乐令有什么用?"},{"id":"6","question":"令牌积分有什么用?"},{"id":"7","question":"南部断层在哪?"},{"id":"8","question":"大妖魔令牌交给谁?"},{"id":"9","question":"神工坊在哪?"},{"id":"10","question":"警戒妖珠有什么用?"}]}}
</body>
</html>
⑵ js遍历json对象读数据
通用方法:版
function printAll(obj){
for(var k in obj){
var v=obj[k];
if(typeof(v)!="object"){
document.writeln(k+"是权"+v+"<br/>");
}else{
printAll(v);
}
}
}
printAll(json);
⑶ js遍历json对象为什么多了个undefined
因为你没有给str1初值, 所以str1一开始是undefined
然後undefined和字符串做+操作会把undefined转换为字符串'undefined'後拼接
从而得到你不期望的结果
你需要
varstr1=''
for(keyinjson1){
str1+=key+""+json1[key]+"";
};
⑷ js 怎么遍历json中的json对象里的数据
var jsondata = yourdata;
for(var x in jsondata){
document.write(x+"="+jsondata[x]);
}
如果是jsonarray就外面套一层内for即可容
var jsonarray =yourarray;
for(var i =0;i<jsonarray.length;i++){
var jsonobj = jsonarray[i];
for(var x in jsonobj){
document.write(x+"="+jsonobj[x]);
}
}
⑸ js 遍历 json 对象数据
你这里的jsonstr本身就已经是对象了,而json只是字符串,也就是说对方传递过来的字符串而已。
应该写成
var jsonStr = "{qiyi:'mp4',tudou:'3gp',youku:'mp4'}";
这样可以表示jsonStr是一个字符串,而现在要jsonStr去执行一下就可以了!
如:
var c = eval(jsonStr);
然后就可以调用了!如c.qiyi等等。不过这里不太好理解的,eval就是将jsonStr字符串当指令执行了,将执行结果传递给c而已。换一句方式可能会更好的理解的!
eval(" var c = "+jsonStr+";");
也就是说重新组合一个语句,这样可以直接使用c.qiyi了!
但其实是一样的道理的,只不过很少人会用到第二种!因为如果eval没有顺利执行时,第二种会出现错误,而第一种则会出现undefined的结果,第一种与第二种的区别就是不管是否空值,第一种c对象是存在的!而第二种却未必见得!第二种方案下,执行了一个语句,然后就感觉“凭空”开始使用一个对象了!然后这样虽是不好,其中是说明下边的语句都依赖于eval这样的一个细节语句,所以不是很多的!
也就是说:
var c = eval("{qiyi:'mp4',tudou:'3gp',youku:'mp4'}");
就是将json执行后的结果。由此可以看出你对数据传递上还有些不理解的!
⑹ JS怎么遍历json数组
用 for in 循环 遍历
varstr='[{"name":"宗2瓜","num":"1","price":"122"},{"name":"宗呱呱","num":"1","price":"100"}]';
varxqo=eval('('+str+')');
for(variinxqo){
alert(xqo[i].name);
}
⑺ javascript 如何遍历json格式数据
<script type="text/javascript">
var jsn=
[{"Id":14,"Name":"政治","ParentId":0},{"Id":15,"Name":"法律","ParentId":0},{"Id":16,"Name":"科技","ParentId":0},{"Id":17,"Name":"娱乐","ParentId":0},{"Id":18,"Name":"时尚","ParentId":0},{"Id":19,"Name":"旅游","ParentId":0},{"Id":20,"Name":"教育","ParentId":0},{"Id":21,"Name":"环保","ParentId":0},{"Id":22,"Name":"社会","ParentId":0},{"Id":23,"Name":"心理","ParentId":0},{"Id":24,"Name":"中医","ParentId":0},{"Id":25,"Name":"西医","ParentId":0},{"Id":26,"Name":"社交","ParentId":0},{"Id":27,"Name":"IT","ParentId":0},{"Id":28,"Name":"汽车","ParentId":0}];
document.write('<table border="1">');
document.write('<tr>');
document.write('<td>id</td>');
document.write('<td>name</td>');
document.write('<td>parentid</td>');
document.write('</tr>');
var len = jsn.length;
for(var i=0;i<len;i++) {
document.write('<tr>');
document.write('<td>'+jsn[i]['Id']+'</td>');
document.write('<td>'+jsn[i]['Name']+'</td>');
document.write('<td>'+jsn[i]['ParentId']+'</td>');
document.write('</tr>');
}
document.write('</table>');
</script>
⑻ js遍历json数组,求大神指导
<!doctypehtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<metaname="Generator"content="EditPlus®">
<metaname="Author"content="">
<metaname="Keywords"content="">
<metaname="Description"content="">
<title>Document</title>
</head>
<body>
</body>
<script>
varjson1=[
{"age":5,"num":2,"people":3,"class":9},
{"age":1,"num":3,"people":2,"class":8},
{"age":4,"num":6,"people":5,"class":3}
],
json2=[
{"field":"age","typle":"sum","title":"ss"},
{"field":"num","typle":"sum","title":"ss"},
{"field":"people","typle":"sum","title":"ss"},
{"field":"class","typle":"nosum","title":"ss"}
],
typeArr={'sum':'sum','nosum':''};
functiongetData(json1,json2,typeArr){
varj=0,
len1=json1.length,
len2=json2.length,
o={},
json3=[];
for(;j<len2;j++){
varj2=json2[j];
if(j2.field){
vartype=typeArr[j2.typle],
num=-1,
i=0;
for(varii=0;ii<json3.length;ii++){
varj3=json3[ii];
if(j3.type===type){
o=clone(j3);
num=ii;
}
}
for(;i<len1;i++){
varj1=json1[i];
if(type==='sum'){
o[j2.field]=o[j2.field]?o[j2.field]+j1[j2.field]:j1[j2.field];
}
}
if(type){
o.type=type;
if(num!==-1){
json3[num]=o;
o={};
}else{
json3[json3.length]=o;
}
}
}
}
returnjson3;
}
//深
functionclone(o){
vark,
ret=o,
b;
if(o&&((b=(oinstanceofArray))||oinstanceofObject)){
ret=b?[]:{};
for(kino){
if(o.hasOwnProperty(k)){
ret[k]=clone(o[k]);
}
}
}
returnret;
}
//getresult
console.log(getData(json1,json2,typeArr));
</script>
</html>
⑼ JavaScript遍历JSON数据( {k:”key1",v:"value1"} )中键值的
var json = {a: "aaaa",v: 'vvvv'};
for(var k in json) {
console.log('key is ' + k);
console.log('value is' + json[k]);
}
⑽ js怎样遍历json数组或对象并读取相应的数据
看这样一段函数定义的代码就明回白了function json_to_format_string(obj,deep){ var html = ""; var blank_string = ""; for (var i = 0; i < deep; i++) { 答blank_string += " "; }; for(var name in obj){ if (typeof(obj[name])=="object") { html += blank_string + name +": "+ "
"; html += json_to_format_string(obj[name],deep+1); }else{ html += blank_string + name +": "+ obj[name]+"
"; } } return html;}