这是我到目前为止。请阅读代码中的评论。它包含我的问题。如何显示JSON对象的值?
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
}
这是我的json对象应该是这样的:{"3":"Stanley Furniture","2":"Shaw","1":"First Quality"}
。我最终想要的是,如果我通过关键3
,我想得到Stanley Furniture
回来,如果我通过Stanley Furniture
,我得到了3
回来。由于3
是customerId,Stanley Furniture
是我的数据库中的customerName。
什么? JSON是JavaScript对象的字符串表示形式,因此如果您想以常规对象的形式访问它,它*就会*需要解析... – James 2010-03-03 17:44:06
'$ .getJSON'已经这样做了。 – BalusC 2010-03-03 17:45:12
啊,好点。 – James 2010-03-03 17:51:52