nonamenonumber
nonamenonumber

Reputation: 31

JSON Chrome Extension Problem

I am creating a google chrome extension as a project and have run into a hurdle. I am playing around with JSON (hoping to eventually use an API i like). I have an example JSON file, load it, get a variable storing the parsed JSON (which I can see as an object, containing objects and values), however, I don't know how to reference values of the objects within.

var xhr = new XMLHttpRequest();
var resp;
xhr.open("GET", "eg.json", true);
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
    resp = JSON.parse(xhr.responseText);
  }
}
xhr.send();

The object resp looks like this after send is executed.

I guess this is because I'm new to JavaScript but how would I get the createdDate as a String variable?

Thanks

Upvotes: 0

Views: 1472

Answers (1)

Nick Craver
Nick Craver

Reputation: 630389

You can access it via dot or bracket (more for dynamic properties) notation (or a mix of both) like any other nested objects. For example, based on the image you posted it would be:

var createdDate = resp.WhoisRecord.audit.createdDate;

Note you need to call this inside that if where resp is defined, only in that callback will it be populated...if you try and use it after your xhr.onreadystatechange = function hookup, it won't be ready when it runs, inside that callback with a readyState == 4, it should be.

Upvotes: 1

Related Questions