在javascript中从json获取值
Hi I have a simple JSON output:
嗨,我有一个简单的JSON输出:
"{
\"max_output_watts\": 150,
\"frame_length_inches\": \"62.20\",
\"frame_width_inches\": \"31.81\"
}"
which I use in a function like so:
我在这样的函数中使用它:
...
$.getJSON(pop_model_dims_url, {model_id: selected}, function(data, jqXHR){
alert(data.max_output_watts);
});
...
The alert is just a placeholder for now, but I don't understand why the alert value is 'undefined'. If I do:
警报现在只是一个占位符,但我不明白为什么警报值是'未定义'。如果我做:
...
$.getJSON(pop_model_dims_url, {model_id: selected}, function(data, jqXHR){
alert(data);
});
...
I get the full json dump as expected.
我按预期得到了完整的json转储。
Any help much appreciated.
任何帮助非常感谢。
4 个解决方案
#1
5
Since your alert(data);
gives you the full JSON dump, this likely means that the JSON has been double encoded on the server.
自你的警报(数据);为您提供完整的JSON转储,这可能意味着JSON已在服务器上进行了双重编码。
jQuery will have parsed it once for you, but you'd need to parse it again.
jQuery会为你解析一次,但是你需要再次解析它。
$.getJSON(pop_model_dims_url, {model_id: selected}, function(data, jqXHR){
data = $.parseJSON(data);
alert(data.max_output_watts);
});
Of course this is not a proper fix. The proper solution is to fix it on the server.
当然这不是一个正确的解决方案。正确的解决方案是将其修复在服务器上。
Once you've done that, you won't need $.parseJSON
anymore.
完成后,您将不再需要$ .parseJSON。
更多相关文章
- 我如何捕获并插入Meteor.Error警报从Meteor.Methods到客户端数据
- Javascript警报中文本的颜色
- 等待执行所有ajax回调的最佳解决方案
- 【问题解决方案】ImportError: No module named 'pygal'
- Python 黏包及黏包解决方案
- 无法安装ndg-httpsclient或者我的解决方案错误
- Linux无法连接网络解决方案
- Media-S 简介(一个开源的DRM解决方案)
- AppScan安全问题解决方案