gpt4 book ai didi

javascript - jQuery 如何检索和警报 JSON 数据

转载 作者:行者123 更新时间:2023-12-03 03:16:46 24 4
gpt4 key购买 nike

JSON:

[  
{
"ID":"25",
"Serial":"1",
"Purchase_id":"8",
"Item":"23",
"Unit":"1",
"HSN":"84212120",
"Quantity":"10",
"Purchase_rate":"100",
"Discount":"10",
"Discount_2":"5",
"Net_rate":"85.5",
"CGST_Percentage":"9",
"SGST_Percentage":"9",
"IGST_Percentage":"0",
"Rate_after_tax":"100.89",
"CGST":"76.95",
"SGST":"76.95",
"IGST":"0",
"Net_amount_without_tax":"855",
"Net_amount":"1008.9"
}
]

jQuery:

$.ajax({
method: "POST",
url: formsubmission,
data: data,
success: function(response) {
var data = JSON.parse(response);
alert(data.ID);
}
})

任何人都可以帮助我为什么警报会带有未定义的消息。如何解析 i8t 以及如何提醒 25 而不是 undefined?

最佳答案

该对象位于解析响应的结果 Array 的索引 0 处。您可以使用括号表示法来引用 JavaScript 对象的索引 0 处的元素

let response =`[{"ID":"25","Serial":"1","Purchase_id":"8","Item":"23","Unit":"1","HSN":"84212120","Quantity":"10","Purchase_rate":"100","Discount":"10","Discount_2":"5","Net_rate":"85.5","CGST_Percentage":"9","SGST_Percentage":"9","IGST_Percentage":"0","Rate_after_tax":"100.89","CGST":"76.95","SGST":"76.95","IGST":"0","Net_amount_without_tax":"855","Net_amount":"1008.9"}]`;

let data = JSON.parse(response);

alert(data[0].ID);

关于javascript - jQuery 如何检索和警报 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46741826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com