gpt4 book ai didi

javascript - 我有一个 JSON 响应,但无法解析它。需要帮助。没有 JQUERY 没有原型(prototype)。只是Javascript

转载 作者:行者123 更新时间:2023-11-29 22:25:37 25 4
gpt4 key购买 nike

所以我对 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"]}})

和解析 JSON 的 JS

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);
}
}
}

** 我希望这可以帮助有完整解决方案的人,因为到处都是部分帖子。**

最佳答案

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);
}
}

关于javascript - 我有一个 JSON 响应,但无法解析它。需要帮助。没有 JQUERY 没有原型(prototype)。只是Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9628809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com