【问题标题】:How do i store JSON from a link to java script array?如何将 JSON 从链接存储到 javascript 数组?
【发布时间】:2013-11-16 11:52:53
【问题描述】:

嗨,我正在尝试将此 JSON 存储到我的 javascript 数组中。有什么办法可以做到这一点。我使用以下代码,但它根本不起作用。

我的 json JSON

 [{"id":"1","Intensity":"1","Location":"12.48,77.26"},{"id":"2","Intensity":"2","Location":"12.47,77.26"},{"id":"3","Intensity":"2","Location":"12.47,77.27"},{"id":"4","Intensity":"2","Location":"12.46,77.24"},{"id":"5","Intensity":"2","Location":"12.44,77.24"},{"id":"6","Intensity":"2","Location":"12.44,77.28"},{"id":"7","Intensity":"2","Location":"12.50,77.28"},{"id":"8","Intensity":"2","Location":"12.45,77.30"},{"id":"9","Intensity":"2","Location":"12.41,77.21"}]

我用 JS 获取 json 数据的 html 代码

<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://code.jquery.com/jquery-migrate-1.1.0.min.js"></script>
</head>
<body>
    <div id="response">werer</div>
    <script>
$.get('http://zonewebs.org/website/naveen/source/location.php', function(data) {
  $('#response').append(data);

});
</script>
</dody>
</html>

【问题讨论】:

    标签: javascript php html arrays json


    【解决方案1】:

    使用 JSON.parse 将 valid json 转换为 javascript 数组

    var dataArray = JSON.parse(data);
    

    【讨论】:

    • 你将使用上面的代码将你的json数据放入一个javascript数组中。然后你可以随意处理它
    • 当我看到您的 json 响应时,json 响应中没有属性。代替 data.property 使用数据
    • 但它不起作用我得到空白页而不是 jason 数据
    • 您正在使用 data.property。使用数据
    • $('#response').append(data);??
    【解决方案2】:

    您的 json 无效。Try jsonlint。从最后删除它:

    {"var":"stuff"}
    

    然后使用:

    JSON.parse(data);
    

    【讨论】:

    • 我怎样才能删除那部分??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2014-06-26
    • 2012-07-10
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多