parseJSON解码阵列
问题描述:
我第一做了MySQL查询和存储在exArray行如下:parseJSON解码阵列
$exArray = array();
index = 0;
while($row = mysql_fetch_assoc($result))
{
$exArray[$index] = $row; //Total of three rows
$index++;
}
然后,我用
json_encode($exArray);
[{ “第一”: “001”,”第二个“:”002“},{”first“:”003“,”second“:”004“}]
注意:我的query.php中的数据包含更多元素。为简洁起见,我在这里缩短了它。实际上,它是三行八列的阵列。在这里,我显示了两行两列。
这是我试过的。
首先,我尝试使用下面的代码并警告“001”没有成功。警报根本不会在我的屏幕上弹出。
$(document).ready(function() {
$.getJSON('query.php', function(data) {
if(data)
{
var obj = jQuery.parseJSON(data);
alert(obj[0].first);
}
});
});
我也试过这种方法。我想将this.first推入数组中。我试图将“001”追加到class ='test'的段落中。但是,我也没有成功。
$(document).ready(function() {
$.getJSON('query.php', function(data) {
if(data)
{
$.each(data, function(){
$(this.first).appendTo(".test")
}
});
});
您的帮助非常值得欢迎。谢谢。
答
在PHP文件,我先叫,
json_encode($exArray);
然后,
$(document).ready(function() {
$.getJSON('vconj.php', function(data) {
if(data)
{
alert(data[0].first);
}
});
});
通知001成功。没有必要使用jQuery.parseJSON(数据)。
谢谢。
'$ .each()'函数中的'data'是什么?你能发布你的确切代码,预期结果和实际结果吗? –
你确定'data'是一个json字符串,而不仅仅是一个数组。 – Musa
请显示实际问题 - “data”的样本值工作正常(按预期提示显示001)。最有可能的'数据'包含不同的东西。 –