Printing plain JSON content to an html page in Javascript / jQuery

After receiving an HTTP response in the form of a JSON file, how can I handle my simple content using jQuery?

I have done this before, but I just can’t understand how it is now.

I use this function to retrieve JSON content.

var json = $.getJSON("test.json",  
   function(response){
           // do stuff
       }
);

Of course, I can process the data contained in JSON, but I would like to process and print my simple content, for example:

{"name": "Pepe","age" : "20"}

Following

alert(response);

Just gives me [object Object]

And this one

alert(jQuery.parseJSON(json));

Just gives me null

I can not find the answer anywhere. I am new to this, so I have to use the wrong search terms because it looks like a trivial question.

+5
source share
3 answers

$.getJSON 3 . data, textStatus jqXHR.

jqXHR responseText, JSON.

var json = $.getJSON("test.json",  
   function(response, status, jqXHR){
           // do stuff
           console.log(jqXHR.responseText);
       }
);
+8

JSON.stringify, , , . MDN

+9

Instead of using alerts to view the results, how about using the console and then using the John Resig solution to record such data?

// usage: log('inside coolFunc',this,arguments);
// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};

And then:

window.log(response);

Taken from: http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/

+1
source

All Articles