Javascript 如何显示 JSON 对象的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2373524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I display values of an JSON object?
提问by Thang Pham
Here is what I got so far. Please read the comment in the code. It contains my questions.
这是我到目前为止所得到的。请阅读代码中的注释。它包含我的问题。
var customer; //global variable
function getCustomerOption(ddId){
$.getJSON("http://localhost:8080/WebApps/DDListJASON?dd="+ddId, function(opts) {
$('>option', dd).remove(); // Remove all the previous option of the drop down
if(opts){
customer = jQuery.parseJSON(opts); //Attempt to parse the JSON Object.
}
});
}
function getFacilityOption(){
//How do I display the value of "customer" here. If I use alert(customer), I got null
}
Here is what my json object should look like: {"3":"Stanley Furniture","2":"Shaw","1":"First Quality"}. What I ultimately want is that, if I pass in key 3, I want to get Stanley Furnitureback, and if I pass in Stanley Furniture, I got a 3back. Since 3is the customerId and Stanley Furnitureis customerName in my database.
这里是我的JSON对象应该是什么样子:{"3":"Stanley Furniture","2":"Shaw","1":"First Quality"}。我最终想要的是,如果我传入 key 3,我想Stanley Furniture回来,如果我传入Stanley Furniture,我就3回来了。因为3是Stanley Furniture我的数据库中的 customerId 和customerName。
回答by BalusC
If the servlet alreadyreturns JSON (as the URL seem to suggest), you don't need to parse it in jQuery's $.getJSON()function, but just handleit as JSON. Get rid of that jQuery.parseJSON(). It would make things potentially more worse. The getFacilityOption()function should be used as callback function of $.getJSON()or you need to write its logic in the function(opts)(which is actually the current callback function).
如果 servlet已经返回 JSON(正如 URL 所暗示的那样),您不需要在 jQuery 的$.getJSON()函数中解析它,而只需将它作为 JSON处理。摆脱那个jQuery.parseJSON()。这可能会使事情变得更糟。该getFacilityOption()函数应该作为的回调函数$.getJSON()或者你需要在function(opts)(实际上是当前的回调函数)中编写它的逻辑。
A JSON string of
一个 JSON 字符串
{"3":"Stanley Furniture","2":"Shaw","1":"First Quality"}
...would return "Stanley Furniture" when accessed as follows
...按如下方式访问时将返回“Stanley Furniture”
var json = {"3":"Stanley Furniture","2":"Shaw","1":"First Quality"};
alert(json['3']);
// or
var key = '3';
alert(json[key]);
To learn more about JSON, I strongly recommend to go through this article. To learn more about $.getJSON, check its documentation.
回答by James
getJSONwill fire an asynchronous XHR request. Since it's asynchronous there is no telling when it will complete, and that's why you pass a callback to getJSON-- so that jQuery can let you know when it's done. So, the variable customeris only assigned once the request has completed, and not a moment before.
getJSON将触发异步 XHR 请求。因为它是异步的,所以不知道它什么时候完成,这就是为什么你传递一个回调到getJSON-- 这样 jQuery 可以让你知道它什么时候完成。因此,customer仅在请求完成后才分配变量,而不是在此之前。
parseJSONreturns a JavaScript object:
parseJSON返回一个 JavaScript 对象:
var parsed = jQuery.parseJSON('{"foo":"bar"}');
alert(parsed.foo); // => alerts "bar"
.. but, as BalusC has said, you don't need to parse anything since jQuery does that for you and then passes the resulting JS object to your callback function.
.. 但是,正如 BalusC 所说,您不需要解析任何东西,因为 jQuery 会为您完成解析,然后将生成的 JS 对象传递给您的回调函数。
回答by Keyne Viana
var customer; //global variable
function getCustomerOption(ddId){
$.getJSON("http://localhost:8080/WebApps/DDListJASON?dd="+ddId, function(opts) {
$('>option', dd).remove(); // Remove all the previous option of the drop down
if(opts){
customer = opts; //Attempt to parse the JSON Object.
}
});
}
function getFacilityOption(){
for(key in costumer)
{
alert(key + ':' + costumer[key]);
}
}

