-3

I m retrieving some json data from database....and want to print that in html output...

Basically i need html output of json data...

ho can i convert json to html.....

my ajax code by which i am getting json data..

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

my php code

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

my json return format

{"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}}]}
kero
  • 10,647
  • 5
  • 41
  • 51
user3819235
  • 31
  • 2
  • 5
  • If you just want the json on a page: http://php.net/manual/en/function.var-dump.php – user887675 Jul 09 '14 at 13:20
  • If you add ```dataType: 'json'``` to your request the response will be a JSON object. But we are not psychics here. We don't know how you intend to structure your JSON. ¿Tables, divs? ¿With what hierarchy? – ffflabs Jul 09 '14 at 13:21
  • you can use `JSON.stringify` to convert your variable `data1` into string and add that to any html element like `$('#myELement').html(JSON.stringify(data1))` – nraina Jul 09 '14 at 13:25
  • You need to print the entire response..?? – MixedVeg Jul 09 '14 at 13:48

2 Answers2

0

You can use the JSON.stringify function with unformatted JSON. It outputs it in a formatted way.

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

This turns

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

into

{
 "foo": "sample", 
 "bar": "sample" 
}
Djouuuuh
  • 1,147
  • 8
  • 16
0

If you simply want to print the returned value as a simple string, you don't have to call JSON.parse(data), since it is already received as a string.

If you to format it in a pretty fashion, you will want a library. Try this answer.

Community
  • 1
  • 1
user3820547
  • 359
  • 1
  • 5