JSON Chrome扩展问题 [英] JSON Chrome Extension Problem
问题描述
我正在创建一个谷歌浏览器扩展程序作为一个项目,并且遇到了障碍。我正在玩JSON(希望最终使用我喜欢的API)。我有一个示例JSON文件,加载它,获取存储解析JSON的变量(我可以看到它作为一个对象,包含对象和值),但是,我不知道如何引用其中的对象的值。
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();
对象resp看起来像这个发送后执行。
The object resp looks like this after send is executed.
我想这是因为我是新的到JavaScript,但我怎么会得到createdDate作为一个字符串变量?
I guess this is because I'm new to JavaScript but how would I get the createdDate as a String variable?
谢谢
Thanks
推荐答案
您可以通过点或< (或更多的动态属性)符号(或两者的组合) )像任何其他嵌套对象。例如,根据您发布的图片,它会是:
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;
请注意,如果需要在 >其中定义了
resp
,只有在该回调中才会填充它......如果您尝试并在 之后使用 xhr.onreadystatechange =
function hookup,它在运行时不会准备好,在 readySTate == 4
,它应该是。
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.
这篇关于JSON Chrome扩展问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!