Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jquery中将json对象转换为html_Javascript_Php_Jquery_Html_Json - Fatal编程技术网

Javascript 如何在jquery中将json对象转换为html

Javascript 如何在jquery中将json对象转换为html,javascript,php,jquery,html,json,Javascript,Php,Jquery,Html,Json,我正在从数据库中检索一些json数据…并希望在html输出中打印这些数据 基本上我需要json数据的html输出 我可以把json转换成html吗 我获取json数据的ajax代码 show = function (id){ $.ajax({ type: "POST", url: ajax_url_print,

我正在从数据库中检索一些json数据…并希望在html输出中打印这些数据

基本上我需要json数据的html输出

我可以把json转换成html吗

我获取json数据的ajax代码

show = function (id){
            $.ajax({
                        type: "POST",              
                        url: ajax_url_print,       
                        data: "action=load&id="+id,

                        success: function(data) {
                        var data1 = JSON.parse(data)
                       // i need here output in html..and want to show on html by id
                        },
                        error: function() {
                            //alert('some error has occured...');
                        },
                        start: function() {
                            //alert('ajax has been started...');    
                        }
                    });
            }
我的php代码

public function getData(){
        if($this->input->post('action') == 'load') {
            $id = $this->input->post('id');
            if($id !=''){
                $query = "SELECT VIEWS FROM wl_views WHERE ID = $id ";

                $row = $this->db->query($query);
                $res = $row->result();

                $result = $res[0]->VIEWS;

                header('Content-Type: application/json');
            echo json_encode(stripslashes($result));
                }
            }
        }   
我的json返回格式

{"title":"gift1","elements":[{"title":"Any Text","source":"Default Text","parameters":{"x":243,"y":181,"colors":["#000000"],"removable":true,"draggable":true,"rotatable":true,"resizable":true,"scale":1,"degree":0,"price":0,"boundingBox":false,"source":"Default Text","originX":243,"originY":181,"currentColor":"#000000","text":"Default Text","font":"Arial","textSize":18}},{"title":"gift1","source":"http://localhost/amazon/uploaded_files/thumb_cache/thumb_600_300_gift173OO.jpg","parameters":{"x":100,"y":81,"colors":false,"removable":false,"draggable":false,"rotatable":false,"resizable":false,"scale":1,"degree":0,"price":0,"boundingBox":false,"source":"http://localhost/amazon/uploaded_files/thumb_cache/thumb_600_300_gift173OO.jpg","originX":100,"originY":81,"originWidth":400,"originHeight":300,"width":400,"height":300}}]}

您可以对未格式化的JSON使用JSON.stringify函数。它以格式化的方式输出它

JSON.stringify({ foo: "sample", bar: "sample" }, null, 4)
这变成

{ "foo": "sample", "bar": "sample" }
进入


如果您只是想将返回的值打印为一个简单的字符串,则不必调用JSON.parse(data),因为它已经作为字符串接收


如果你想以一种非常时尚的方式格式化它,你会想要一个库。尝试回答。

如果您只想在页面上显示json:如果您将
数据类型:“json”
添加到您的请求中,响应将是一个json对象。但我们不是通灵者。我们不知道您打算如何构造JSON。桌子,沙发??采用什么层次结构?您可以使用
JSON.stringify
将变量
data1
转换为字符串,并将其添加到任何html元素中,如
$('.#myELement').html(JSON.stringify(data1))
您需要打印整个响应。。??
{
 "foo": "sample", 
 "bar": "sample" 
}