返回结果会更改javascript JSON字符串中的数据
我有以下代码:返回结果会更改javascript JSON字符串中的数据,javascript,json,Javascript,Json,我有以下代码: 函数bestMove(){ 变量url=”http://www.api.com“;//应该以json格式发送{“location”:4}。 $.getJSON(url、函数(数据){ 控制台日志(数据); 返回数据; }); } $(函数(){ console.log(bestMove()) //司机代码在这里 }); 您应该能够对代码进行以下更新,以实现您的目标: function bestMove() { var url = "http://www.api.com";
函数bestMove(){
变量url=”http://www.api.com“;//应该以json格式发送{“location”:4}。
$.getJSON(url、函数(数据){
控制台日志(数据);
返回数据;
});
}
$(函数(){
console.log(bestMove())
//司机代码在这里
});代码>
您应该能够对代码进行以下更新,以实现您的目标:
function bestMove() {
var url = "http://www.api.com"; // supposed to send {"location":4} in json format.
// Return the result of $.getJSON
return $.getJSON(url, function(data) {
console.log(data);
return data;
});
}
$(function() {
// Add a then handler after bestMove() is called. The function
// handler will give you access to driver data returned from to
// ajax response
bestMove().then(function(driver) {
//driver code here
console.log(driver)
})
});
您应该能够对代码进行以下更新,以实现您的目标:
function bestMove() {
var url = "http://www.api.com"; // supposed to send {"location":4} in json format.
// Return the result of $.getJSON
return $.getJSON(url, function(data) {
console.log(data);
return data;
});
}
$(function() {
// Add a then handler after bestMove() is called. The function
// handler will give you access to driver data returned from to
// ajax response
bestMove().then(function(driver) {
//driver code here
console.log(driver)
})
});
您需要像这样更新代码
function bestMove(callback) {
var url = "http://www.api.com"; // supposed to send {"location":4} in json format.
$.getJSON(url, function(data) {
return callback(null, data);
});
}
$(function() {
bestMove(function(err, data){
console.log(data);
});
});
这里我首先使用错误回调。
在回调函数中获得的数据是已经解析的Javascript对象,因此无需解析该对象。您可以直接将其与“.”运算符或“[]”一起使用以访问属性。您需要像这样更新代码
function bestMove(callback) {
var url = "http://www.api.com"; // supposed to send {"location":4} in json format.
$.getJSON(url, function(data) {
return callback(null, data);
});
}
$(function() {
bestMove(function(err, data){
console.log(data);
});
});
这里我首先使用错误回调。
在回调函数中获得的数据是已经解析的Javascript对象,因此无需解析该对象。您可以直接将其与“.”运算符或“[]”一起使用来访问该属性。如果答案有点明显,请重复“对不起”,但我仍然无法使用JSON.parse(driver)
解析数据。谢谢你的快速回复thou@LuketheGeek{location:4}
是一个JavaScript对象,而不是JSON<代码>$。getJSON
已在为您分析结果。您正在尝试解析已解析的内容:)@TylerRoper但是我如何才能获取位置的值呢?@LuketheGeek它是一个对象,因此您可以执行objectName.propertyName
或objectName[“propertyName”]
。如果您的对象名为data
,而您想要的属性是location
,那么您应该使用data.location
。通常认为简单地给出解决某人问题的代码是不好的做法。如果答案有点明显,请见谅,但我仍然无法使用JSON.parse(driver)
解析数据。谢谢你的快速回复thou@LuketheGeek{location:4}
是一个JavaScript对象,而不是JSON<代码>$。getJSON
已在为您分析结果。您正在尝试解析已解析的内容:)@TylerRoper但是我如何才能获取位置的值呢?@LuketheGeek它是一个对象,因此您可以执行objectName.propertyName
或objectName[“propertyName”]
。如果您的对象名为data
,而您想要的属性是location
,那么您应该使用data.location
。通常认为简单地给出解决某人问题的代码是不好的做法。请看,简单地给出解决某人问题的代码通常被认为是不好的做法。见注。请考虑一下。让我知道为什么已经被否决了。因此,我会注意到这一点。我对你的答案投了反对票,因为你没有给出任何解释——正如已经指出的,这只是代码。这是伟大的前进,你会更清楚,但如果你想有这个答案的否决票删除,那么你应该编辑它,以提供更多的细节。(编辑:你提供了一些细节,所以我现在删除否决票。)Ohhk。我对否决票没有异议,我只是想知道原因。我会知道这一点的。非常感谢。简单地给出解决某人问题的代码通常被认为是不好的做法。见注。请考虑一下。让我知道为什么已经被否决了。因此,我会注意到这一点。我对你的答案投了反对票,因为你没有给出任何解释——正如已经指出的,这只是代码。这是伟大的前进,你会更清楚,但如果你想有这个答案的否决票删除,那么你应该编辑它,以提供更多的细节。(编辑:你提供了一些细节,所以我现在删除否决票。)Ohhk。我对否决票没有异议,我只是想知道原因。我会知道这一点的。非常感谢你。