Javascript Ajax:从json行中提取嵌套变量
我想从json行中提取特定值(cownumber、weight、height) 当使用Postman从url访问JSON时,看起来是这样的Javascript Ajax:从json行中提取嵌套变量,javascript,jquery,python,json,ajax,Javascript,Jquery,Python,Json,Ajax,我想从json行中提取特定值(cownumber、weight、height) 当使用Postman从url访问JSON时,看起来是这样的 { "data": { "attributes": { "cownumber": "1234", "weight": 300, "height": 25 }, "type": "master_animal" }, "links": { "self": "/master_anima
{
"data": {
"attributes": {
"cownumber": "1234",
"weight": 300,
"height": 25
},
"type": "master_animal"
},
"links": {
"self": "/master_animal/1234"
}
}
我使用一个函数从URL中提取cownumber
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
</script>
函数getQueryVariable(变量)
{
var query=window.location.search.substring(1);
var vars=query.split(&);
对于(var i=0;i你试过吗
$(document).ready(function(){
$('#load').click(function(){
let cownumber = getQueryVariable("cownumber")
$.ajax({
url: '/api/master_animal/'+cownumber,
type: 'GET',
dataType : 'json',
success: function(response) {
data = response['data']['attributes'];
let height = $(data['height']);
let weight = $(data['weight']);
$('#cownumber').val(cownumber);
$('#height').val(height);
$('#weight').val(weight);
console.log(data);
},
error: function(error) {
alert(JSON.stringify(error));
}
});
});
});
$(文档).ready(函数(){
$('#加载')。单击(函数(){
让cownumber=getQueryVariable(“cownumber”)
$.ajax({
url:'/api/master_animal/'+cownumber,
键入:“GET”,
数据类型:“json”,
成功:功能(响应){
数据=响应['data']['attributes'];
设高度=$(数据['height']);
让权重=$(数据['weight']);
$('#cownumber').val(cownumber);
$('高度').val(高度);
$('重量').val(重量);
控制台日志(数据);
},
错误:函数(错误){
警报(JSON.stringify(错误));
}
});
});
});
首先,您访问数据时出错。仅仅因为变量名为
data
,并不意味着您已经在data
属性中访问了数据。这意味着您需要像这样访问数据
var height = data.data.attributes.height;
另外,您不需要在jQuery中包装您的值,只需获取值并使用它即可
var height = data.data.attributes.height;
$('#height').val(height);
height
和weight
属性进一步嵌套在响应对象中的属性中:
success: function(response) {
var height = response.data.attributes.height;
var weight = response.data.attributes.weight;
$('#cownumber').val(cownumber);
$('#height').val(height);
$('#weight').val(weight);
},
使用此utils函数可从嵌套对象中提取任何值
getValueAtPath(obj, path, def){
if(!obj) return def;
path = path.split('.');
let i, len;
len = path.length;
for(i = 0; i < len; i++){
if(!obj || typeof obj !== 'object') return def;
obj = obj[path[i]];
}
if (obj === undefined) return def;
return obj;
}
希望这有帮助;进行了您建议的更改,但得到了一个不同的错误“捕获类型错误:无法读取未定义的@samueloyeleye的属性'attributes',那么您的JSON结构不是您发布的内容。console.log(data)
记录您的数据
变量,并确保它包含您认为的功能
getValueAtPath(obj, path, def){
if(!obj) return def;
path = path.split('.');
let i, len;
len = path.length;
for(i = 0; i < len; i++){
if(!obj || typeof obj !== 'object') return def;
obj = obj[path[i]];
}
if (obj === undefined) return def;
return obj;
}
let cowNumber = getValueatPath(jsonData, 'data.attributs.cownumber', defualtvalue)
let height = getValueatPath(jsonData, 'data.attributs.height', defualtvalue)
let weight = getValueatPath(jsonData, 'data.attributs.weight', defualtvalue)