Can';似乎无法在Javascript中处理JSON响应
我有一个来自Amazon API的JSON响应,响应如下:Can';似乎无法在Javascript中处理JSON响应,javascript,json,Javascript,Json,我有一个来自Amazon API的JSON响应,响应如下: { "Result": { "Data": { "Title": "HALO 3 (XBOX 360 - REGION FREE)", "FormattedPrice": "$19.95", "Source": "Product Description", "Content": "The epic saga continues..." } } } 在我的J
{
"Result": {
"Data": {
"Title": "HALO 3 (XBOX 360 - REGION FREE)",
"FormattedPrice": "$19.95",
"Source": "Product Description",
"Content": "The epic saga continues..."
}
}
}
在我的Javascript代码中,当试图访问FormattedPrice时,我一直没有定义
我已经尝试过各种版本:
var price1 = Response.Result.Data.FormattedPrice;
var price1 = Response.Result[0].Data[0].FormattedPrice;
var price1 = Response.Result[0].FormattedPrice;
添加了完整的呼叫代码:
data.name=Halo 3。更改了对PriceResponse的响应
//Load Amazon Price data
var setUrl =
"https://example.com/price.php?q="+encodeURI(data.name)+"&output=json";
console.log(setUrl);
jQuery.getJSON(setUrl, function(priceResponse) {
var price1 = Response.Result[0].Data[0].FormattedPrice;
jQuery("#info-link-amazon").append('<span style="float:right;">$'+price1+'</span>');
});
//加载亚马逊价格数据
var setUrl=
"https://example.com/price.php?q=“+encodeURI(data.name)+”&output=json”;
console.log(setUrl);
getJSON(setUrl,函数(priceResponse){
var price1=Response.Result[0]。数据[0]。FormattedPrice;
jQuery(“亚马逊信息链接”).append(“$”+price1+”);
});
首先,使用浏览器的开发人员工具调试代码。getJSON调用完成后的Whatch priceResponse值,并验证(在网络选项卡上)调用的响应是否符合预期。然后,根据您已发布的响应,必须使用priceResponse变量访问响应:
//Load Amazon Price data
var setUrl = "https://example.com/price.php?q="+encodeURI(data.name)+"&output=json";
console.log(setUrl);
jQuery.getJSON(setUrl, function(priceResponse) {
var price1 = priceResponse.Result.Data.FormattedPrice;
});
JSON已经被解析了吗?1)不适合将变量命名为全局变量(
Response
)。2) 确保它被解析,并像其他任何事情一样进行调试:记录变量的值和类型如果我尝试解析响应,我会得到以下错误:JSON.parse:JSON数据第1行第2列出现意外字符。你能发布获取数据和解析数据的代码吗?现在为你添加!谢谢。你首先收到的错误是什么。基于您的代码,没有称为“Response”的变量,我重新发布响应是为了满足您的期望。谢谢!我这样做了,效果非常好。我真的觉得我这样做了,它返回了未定义的,但我可能没有给它足够的时间来正确刷新。非常感谢。