簡體   English   中英

如何在我的HTML網頁中使用JSON文件

[英]How to Use the JSON file in my HTML web page

我正在嘗試在HTML頁面中實現以下JSON文件。

<html>
  <head>
    <title> test get</title>

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">

      <script>
        $(function() {
            $.getJSON('json.json',function(data) {
                $.each(data.quotes,function(key,value) {
                    alert( key+ "said by "+value);
                });
            });
        });
    </script>

  </head>

  <body>
  </body>

</html>

這是我正在處理的JSON

{
    "quotes": {
        "hey there":"randomguy1",
        "wassup":"randomguy2",
        "coool":"randomguy3"
    }
}

我檢查了不同的教程,並且在stackoverflow上也遇到了類似的問題,但仍然無法弄清錯誤。

只需修復您的script代碼,

必須關閉<script ...jquery>標簽。

<html>
<head>
    <title>test get</title>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

  <script>
    $(function () {
        $.getJSON('json.json', function (data)
        {
            $.each(data.quotes, function (key, value) {
                alert(key + "said by " + value);
            });
        });

    });
  </script>

  </head>

  <body> </body>
</html>

您可以通過其他方式實現它。 使用ajax加載文件內容並將其解析為json

$.ajax({
            url : "helloworld.json",
            success : function (data) {
                //TODO parse string to json
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM