Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
281
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@miiitaka

jQuery で JSON ファイルを読み込む

JSONファイル読み込みのおさらいメモ。
下記ファイルを読み込みます。

data.json
[
  {"id":1,"name":"田中"},
  {"id":2,"name":"鈴木"},
  {"id":3,"name":"佐藤"}
]

これを、id を id属性に。name を li タグ内に表示します。

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(function() {
  $.getJSON("data.json" , function(data) {
    var
      ulObj = $("#demo"),
      len = data.length;

    for(var i = 0; i < len; i++) {
      ulObj.append($("<li>").attr({"id":data[i].id}).text(data[i].name));
    }
  });
});
</script>
</head>
<body>
<ul id="demo"></ul>
</body>
</html>
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
281
Help us understand the problem. What are the problem?