HTML中的JSON对象检索

时间:2015-04-22 20:28:58

标签: javascript html json

<!DOCTYPE html>
<html>
<body>

<h2>EPA</h2>

<p id="demo"></p>

<script>
//UV
var text = '{"ZIP_CODE":92507,"UV_INDEX":8,"UV_ALERT":0}'
var obj = JSON.parse(text);

document.getElementById("demo").innerHTML = "UV INDEX" + "<br>" + "Zip code: " + obj.ZIP_CODE + "<br>" + "UV_ INDEX: " + obj.UV_INDEX + "<br>" + "UV_ALERT: " + obj.UV_ALERT + "<br><br>" ; 

</script>

</body>
</html>

目前,我正在输入JSON对象。无论如何我可以从URL下载JSON对象而不是提供它吗?

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery:

// get json data from a url
var urlString = "/example/getdata";
$.getJSON(urlString, function(obj){
    document.getElementById("demo").innerHTML = "UV INDEX" + "<br>" + "Zip     code: " + obj.ZIP_CODE + "<br>" + "UV_ INDEX: " + obj.UV_INDEX + "<br>" + "UV_ALERT: " + obj.UV_ALERT + "<br><br>" ; 
});