所以我有一些带有JSON的问题。
setupdata({"NON-RELIGOUS ORGANIZATIONS":{"23":["NON-RELIGOUS ORGANIZATIONS","Does this Loce test 2","",null]},"YARD SALES":{"1":["YARD SALES","yard sale","1332599400",null],"22":["YARD SALES","LOCKING TEST YARD SALE a change and more","1331814600",null],"21":["YARD SALES","Generic Yard Sale Title","",null]}})
我正在处理....
function setupdata(data){
alert(data.length);
for(i=0; i<data.length; i++) {
newdiv = document.createElement("DIV");
newdiv.setAttribute("id","cat"+i);
newdiv.innerHTML(\'test\');
document.getElementById(\'divmiddle\').appendChild(newdiv);
}
}
我正在为data.length获取UNDEFINED,并且i循环未运行,因为它未定义。我想我对如何解析从JSON调用返回的数据感到困惑。任何帮助适用。
和获胜的答案
用PHP创建数组:
while($getcats = mysqli_fetch_array($query)){
$layout[$getcats[news_cat_name]];
if(!$getstories = mysqli_query($connection,"SELECT news_id,news_title,news_organization,news_expiration from news_items where news_category='$getcats[news_cat_id]'")){ echo mysqli_error($connection);}
while($looptitles=mysqli_fetch_array($getstories)){
$layout[$getcats[news_cat_name]][$looptitles[news_id]][]=date("m/d/y G:i a",$looptitles[news_expiration]);
$layout[$getcats[news_cat_name]][$looptitles[news_id]][]=$looptitles[news_title];
$layout[$getcats[news_cat_name]][$looptitles[news_id]][]=$looptitles[news_organization];
}
}
和结果JSON
setupdata({"NON-RELIGOUS ORGANIZATIONS":{"23":[false,"Does this Loce test 2","2"]},
"YARD SALES":{
"1":["03\/24\/12 10:30 am","yard sale","0"]
,"22":["03\/15\/12 8:30 am","LOCKING TEST YARD SALE a change and more","0"],
"21":[false,"Generic Yard Sale Title","0"]}})
和JS分享JSON
for(var key in data){//OUTER LOOP FOR THE MAIN OBJECTS
divkey = document.createElement("DIV");
divkey.setAttribute("id",key);
divkey.style.fontSize = \'13px\';
divkey.style.textDecoration = \'underline\';
divkey.style.paddingTop = \'3px\';
divkey.style.paddingBottom = \'5px\';
divkey.style.position = \'relative\';
divkey.innerHTML = key;
document.getElementById(\'divmiddle\').appendChild(divkey);
for(var mykey in data[key]){//INNER LOOP FOR THE INNER OBJECTS
divkey = document.createElement("DIV");
divkey.setAttribute("id",mykey);
divkey.style.fontFamily = \'Verdana\';
divkey.style.fontSize = \'11px\';
divkey.style.paddingBottom = \'5px\';
divkey.style.position = \'relative\';
divkey.innerHTML = +data[key][mykey][1]+\' -- \' + data[key][mykey][0];
document.getElementById(\'divmiddle\').appendChild(divkey);
}
}
}
**我希望这可以帮助有完整解决方案的人,因为到处都有部分帖子。**
答案 0 :(得分:1)
基本上{}
是Object
而不是Array
所以它没有length
属性。
但你可以用
循环它for (var key in data) {
// your code ....
}
编辑:回答您的评论
您只需使用此风格
即可使用Object
{'data': { /* your json data */ }} // when there's data
{'data': null} // when no data available
然后在你的功能中。
if (data.data) {
for(var key in data.data) {
// your code
}
}
答案 1 :(得分:1)
setupdata({ "NON-RELIGOUS ORGANIZATIONS": { "23": ["NON-RELIGOUS ORGANIZATIONS", "Does this Loce test 2", "", null] }, "YARD SALES": { "1": ["YARD SALES", "yard sale", "1332599400", null], "22": ["YARD SALES", "LOCKING TEST YARD SALE a change and more", "1331814600", null], "21": ["YARD SALES", "Generic Yard Sale Title", "", null]} });
function setupdata(data) {
alert(JSON.stringify(data));
alert(data.length);
for (var key in data) {
alert(JSON.stringify(data[key]['23']));
newdiv = document.createElement("DIV");
newdiv.setAttribute("id", "cat" + data[key]['23']);
newdiv.innerHTML = 'test';
document.getElementById('divmiddle').appendChild(newdiv);
}
}