无法使用Javascript访问未命名JSON数组中的字段
我对Javascript和JSON相当陌生,我正在尝试访问未命名JSON数组中的值。因为这不是我的JSON文件,所以我不能修改它并给数组命名。 以下是JSON代码:无法使用Javascript访问未命名JSON数组中的字段,javascript,arrays,json,ajax,Javascript,Arrays,Json,Ajax,我对Javascript和JSON相当陌生,我正在尝试访问未命名JSON数组中的值。因为这不是我的JSON文件,所以我不能修改它并给数组命名。 以下是JSON代码: [ { "address":"0x51c3", "Balance":"2425" },{ "address":"0x51c9", "Balance":"2425" } ] 以下是我在Javascript/AJAX方面的尝试: <html> <head>
[
{
"address":"0x51c3",
"Balance":"2425"
},{
"address":"0x51c9",
"Balance":"2425"
}
]
以下是我在Javascript/AJAX方面的尝试:
<html>
<head>
<meta content = "text/html; charset = ISO-8859-1" http-equiv = "content-type">
<script type = "application/javascript">
function loadJSON(){
var data_file = "http://localhost/balance.json";
var http_request = new XMLHttpRequest();
try{
// Opera 8.0+, Firefox, Chrome, Safari
http_request = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
http_request.onreadystatechange = function(){
if (http_request.readyState == 4 ){
// Javascript function JSON.parse to parse JSON data
var jsonObj = JSON.parse(http_request.responseText);
// jsonObj variable now contains the data structure and can
// be accessed as jsonObj.name and jsonObj.country.
document.getElementById("Address").innerHTML = jsonObj[1].address;
document.getElementById("Balance").innerHTML = jsonObj[1].Balance;
}
}
http_request.open("GET", data_file, true);
http_request.send();
}
</script>
</head>
<body>
<table class = "src">
<tr><th>Address</th><th>Balance</th></tr>
<tr><td><div id = "Address"></div></td>
<td><div id = "Balance"></div></td></tr>
</table>
<div class = "central">
<button type = "button" onclick = "loadJSON()">Update Details </button>
</div>
</body>
</html>
函数loadJSON(){
变量数据_文件=”http://localhost/balance.json";
var http_request=new XMLHttpRequest();
试一试{
//Opera 8.0+、Firefox、Chrome、Safari
http_请求=新的XMLHttpRequest();
}捕获(e){
//Internet Explorer浏览器
试一试{
http_请求=新的ActiveXObject(“Msxml2.XMLHTTP”);
}捕获(e){
试一试{
http_请求=新的ActiveXObject(“Microsoft.XMLHTTP”);
}捕获(e){
//出了点问题
警告(“你的浏览器坏了!”);
返回false;
}
}
}
http_request.onreadystatechange=函数(){
如果(http_request.readyState==4){
//Javascript函数JSON.parse解析JSON数据
var jsonObj=JSON.parse(http_request.responseText);
//jsonObj变量现在包含数据结构并可以
//可作为jsonObj.name和jsonObj.country访问。
document.getElementById(“地址”).innerHTML=jsonObj[1]。地址;
document.getElementById(“Balance”).innerHTML=jsonObj[1]。Balance;
}
}
http_request.open(“GET”,数据文件,true);
http_request.send();
}
地址余额
更新详细信息
您的etherBalance在JSON中称为Balance,在DIV ID中也称为Balance
现在插入工作正常了
var data='[{“地址”:“0x51c3”,“余额”:“2425”},{“地址”:“0x51c9”,“余额”:“2425”}];
函数loadJSON(){
var jsonObj=JSON.parse(数据);
//jsonObj变量现在包含数据结构并可以
//可作为jsonObj.name和jsonObj.country访问。
document.getElementById(“地址”).innerHTML=jsonObj[1]。地址;
document.getElementById(“Balance”).innerHTML=jsonObj[1]。Balance;
}
地址
平衡
更新详细信息
etherBalance在JSON中称为Balance,[1]将得到第二个道歉。在上传此代码之前,我已经更改了变量名。尽管如此,问题仍然存在。此外,我对检索第二组值感兴趣。控制台中有错误吗?有没有可能使用jQueryAjax?嗨!这似乎奏效了。谢谢你的帮助:)