我正在将JQuery与从PHP读取MySQL数据库中值的PHP文件进行Ajax调用一起使用。(Wordpress)当值返回到jQuery并将其输出到控制台时,这是获得的示例: 1:{i:0; i:90;}
如何使用jQuery从此序列化数组中访问值90,其中90是一个不同的值,但始终位于同一位置?
$prod = $_POST['item'];
//$mod_prod = substr($prod, 0, strpos($prod, "|"));
//query to get data from the table
$query_Product = sprintf("select meta_value from wp_postmeta Where post_id = '".$prod."' and meta_key = '_upsell_ids'");
//execute query
$result_Product = $mysqli_Product->query($query_Product);
//loop through the returned data
$data_ListRD = array();
foreach ($result_Product as $row) {
$data_ListRD[] = $row;
}
//free memory associated with result
$result_Product->close();
//close connection
$mysqli_Product->close();
//print_r(json_encode($data_ListRD));
//now print the data
print json_encode($data_ListRD);
$ data_ListRD我认为是一个数组,如何对它反序列化?
答案 0 :(得分:0)
如果不使用jQuery,则需要使用$ .parseJSON函数或JSON.parse方法。但是由于您收到的数据类似“ {a:1,b:{i:0,i:90}}”(在您的示例中有错字错误),因此您已经收到一个对象,这就是为什么您不需要解析它,因为它已经不是JSON了。例如,如果您用控制台记录您的结果,它将看起来像这样:
let data = {a:1, b:{i:0,i:90}} //data that comes from AJAX (as an example).
console.log(data);
//Result
a: 1
b: {i: 90}
__proto__: Object