[英]Issue reading from an ext json file to html
So I simply want to get 1 value stored in an external JSON file in the same folder. 因此,我只想获取存储在同一文件夹中的外部JSON文件中的1个值。 Every time I try to get the item, chrome says "unexpected token : " also, it shows another error saying that data in the JSON.parse(data) method is wrong. 每次我尝试获取商品时,chrome也会说“意外令牌:”,这还会显示另一个错误,说明JSON.parse(data)方法中的数据是错误的。 Either way, I'm getting that token error with or without the parse(). 无论哪种方式,无论是否使用parse(),我都将收到该令牌错误。 it doesn't like the "e1" :
part, there is a red error line under that portion of the json file. 它不喜欢"e1" :
部分,在json文件的该部分下面有一条红色错误线。 I've seen about 6 suggestions on this site and others but it didn't help my particular issue. 我在该网站和其他网站上看到了大约6条建议,但对我的特定问题没有帮助。 Any thoughts? 有什么想法吗?
here is my code: 这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Objects 2</title>
<script type="text/javascript" src="data.json"></script>
</head>
<body>
<div id="test"></div>
<script>
var extJSON = JSON.parse(data);
document.getElementByID("test").innerHTML = extJSON.e1.position;
</script>
</body>
</html>
And here is my JSON file 这是我的JSON文件
{
"e1" :
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 42,
"position" : "mechanic",
"phone" :
{
"home" : "718-111-1111",
"mobile" : "718-222-2222",
"work" : "718-333-3333"
}
},
"e2" :
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 34,
"position" : "Investor",
"phone" :
{
"home" : "305-111-1111",
"mobile" : "305-222-2222",
"work" : "305-333-3333"
}
},
"e3" :
{
"first_name" : "Rusty",
"last_name" : "Colon",
"age" : 23,
"position" : "Diver",
"phone" :
{
"home" : "415-111-1111",
"mobile" : "415-222-2222",
"work" : "415-333-3333"
}
},
"e4" :
{
"first_name" : "Anna",
"last_name" : "Spencer",
"age" : 20,
"position" : "Event Planner",
"phone" :
{
"home" : "786-111-1111",
"mobile" : "786-222-2222",
"work" : "786-333-3333"
}
}
}
I simply want to print any value or property value pair. 我只想打印任何值或属性值对。 Thanks. 谢谢。
Try to change your script tag like so: 尝试像这样更改脚本标签:
<script id="data" type="application/json" src="data.json">
then refer to it: 然后参考它:
var extJSON = JSON.parse($("#data").html());
Hope this helps. 希望这可以帮助。
Please try this Code 请尝试此代码
<!DOCTYPE html>
<html>
<head>
<title>Objects 2</title>
<script type="text/javascript" src="data.json"></script>
</head>
<body>
<div id="test"></div>
<script>
var extJSON = data;
document.getElementById("test").innerHTML = extJSON.e1.position;
// it is getElementById not getElementByID. it is case sensitive.
</script>
</body>
</html>
as per your code data.json
file should be like 根据您的代码data.json
文件应该像
data= {
"e1" :
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 42,
"position" : "mechanic",
"phone" :
{
"home" : "718-111-1111",
"mobile" : "718-222-2222",
"work" : "718-333-3333"
}
},
"e2" :
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 34,
"position" : "Investor",
"phone" :
{
"home" : "305-111-1111",
"mobile" : "305-222-2222",
"work" : "305-333-3333"
}
},
"e3" :
{
"first_name" : "Rusty",
"last_name" : "Colon",
"age" : 23,
"position" : "Diver",
"phone" :
{
"home" : "415-111-1111",
"mobile" : "415-222-2222",
"work" : "415-333-3333"
}
},
"e4" :
{
"first_name" : "Anna",
"last_name" : "Spencer",
"age" : 20,
"position" : "Event Planner",
"phone" :
{
"home" : "786-111-1111",
"mobile" : "786-222-2222",
"work" : "786-333-3333"
}
}
};
Hope it works for you. 希望对你有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.