如何在Javascript中打印json数组 [英] How Print json Array in Javascript

查看:180
本文介绍了如何在Javascript中打印json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP:代码:

$category_searchresult = $db->db_query("SELECT category_code, category_name, category_comment FROM qa_categories WHERE ".$search_by." LIKE '%".$search_string."%'");

        $i=1;
        $qa = array();
        while($categories_query_result = mysql_fetch_array($category_searchresult))
        {
            $qa[$i][] = $categories_query_result['category_code'];
            $qa[$i][] = $categories_query_result['category_name'];
            $qa[$i][] = $categories_query_result['category_comment'];

            $i++;
         }
        echo json_encode($qa);

JS代码:

$.ajax({
     type: "POST",
     url: "ajax_js.php",
     data: search_data,
     cache: false,
     success: function(search_result) {
        //Print Here
    });

PHP的当前输出IS:

Current Output IS from PHP:

{"1":["4BA3CC","Fontaneria","Para la Casa"],"2":["CF0345","Herramientas","Herramients de Hogar"],"3":["1265CA","Luces","Luces de todo tipo"],"4":["4C4C9F","Vidrios","Reflectores de auto"]}

非常感谢

推荐答案

添加

dataType: 'json'

到您的.ajax()调用.这将告诉jQuery将PHP中的JSON字符串解码回本机Javascript数据结构.另外,您可以在成功处理程序中使用data参数,然后自己明确进行解码:

to your .ajax() call. That will tell jQuery to decode the JSON string from PHP back into a native Javascript data structure. Alternatively, you can take the data parameter in the success handler and explicitly do the decoding yourself:

success: function(data) {
    var data = jquery.parseJSON(data);
}

这篇关于如何在Javascript中打印json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆