简体   繁体   English

如何使用Javascript或Jquery将一系列JSON数据附加到HTML?

[英]How do I append a range of JSON data to my HTML using Javascript or Jquery?

What I am trying to do is to break-up what I grab from my JSON file. 我想做的是分解从JSON文件中获取的内容。 For example say my JSON data looks like this: 例如说我的JSON数据如下所示:

{
"paintings": {
  "painting": [
    {
    "title": "Boardwalk 5",
    "artist": "Arnie Palmer",
    "image": "ap1.jpg",
    "price": 850
    },
    {
    "title": "A Lasting Piece",
    "artist": "Arnie Palmer",
    "image": "ap2.jpg",
    "price": 450
    },
    {
    "title": "Surf at High Tide",
    "artist": "Arnie Palmer",
    "image": "ap3.jpg",
    "price": 950
    },
    {
    "title": "The Games We Play",
    "artist": "Arnie Palmer",
    "image": "ap4.jpg",
    "price": 850
    }
  ]
} 
}

I want append to <div class="area1"> the first 2 items in my JSON array then append items 3 and 4 to <div class="area2"> . 我想将JSON数组中的前2个项目附加到<div class="area1"> ,然后将项目3和4附加到<div class="area2"> Here is how I am pulling all my data: 这是我提取所有数据的方式:

$(document).ready(function () {
$.ajax({
    type: 'GET',
    url: 'data.json',
    dataType: 'json',
    success: jsonParser
});
});

function jsonParser(json) {
$('#load').fadeOut();

$.getJSON('data.json', function(data){
    $.each(data.paintings.painting, function(k,v){
        var title = v.title;
        var img = v.image;
        var price = v.price;
///here is where I need help
        $('.area1').append('<div class="painting"><img src="images/' + img + '" width="200" height="225" alt="' + title + '" /><br/><div class="title">' + title + '<br/>$' + price + '</div></div>')
    });
});
}

And here is my HTML: 这是我的HTML:

<body>
<div class="area1">...first half of JSON data here... </div>
<div class="area2">... second half of JSON data here </div>
</body>

So one JSON file with the data broken up then appended to two areas 因此,一个JSON文件的数据被分解,然后附加到两个区域

You just need to check the index ( k ) in your each() function to decide which area to add to: 您只需要检查each()函数中的索引( k )即可确定要添加到哪个区域:

$.getJSON('data.json', function(data){
    $.each(data.paintings.painting, function(k,v){
        var title = v.title;
        var img = v.image;
        var price = v.price;

        // k is the index in the array. indexes 0 and 1 go to area1. 2 and 3 go to area2
        var areaid = k < 2 ? '.area1' : '.area2';
        $(areaid).append('<div class="painting"><img src="images/' + img + '" width="200" height="225" alt="' + title + '" /><br/><div class="title">' + title + '<br/>$' + price + '</div></div>')
    });
});

Try this: 尝试这个:

<script>
    var url = "json.json";
    $.getJSON(url, function(data) {
        $.each(data.paintings.painting, function(k, v) {
            var title = v.title;
            var img = v.image;
            var price = v.price;
            if (k < 2) {
                $('.area1').append('<div class="painting"><img src="images/' + img + '" width="200" height="225" alt="' + title + '" /><br/><div class="title">' + title + '<br/>$' + price + '</div></div>');
            } else {
                $('.area2').append('<div class="painting"><img src="images/' + img + '" width="200" height="225" alt="' + title + '" /><br/><div class="title">' + title + '<br/>$' + price + '</div></div>')
            }

        });
    });
</script>

A bit more verbose than @Rhumborls solution. 比@Rhumborls解决方案更详细。

 $.getJSON('data.json', function(data) { $.each(data.paintings.painting, function(k, v) { var title = v.title; var img = v.image; var price = v.price; var areaid; switch (k) { case 0: case 1: areaid = '.area1'; break; case 2: case 3: areaid = '.area2'; break; } $(areaid).append('<div class="painting"><img src="images/' + img + '" width="200" height="225" alt="' + title + '" /><br/><div class="title">' + title + '<br/>$' + price + '</div></div>') }); }); 

Or even more scalable 甚至更具可扩展性

 $.getJSON('data.json', function(data) { $.each(data.paintings.painting, function(k, v) { var title = v.title; var img = v.image; var price = v.price; // We figure out the area to append to by rounding down to nearest 10 and plus by 1 // key of 0-9 becomes 1, key of 10-19 becomes 2 ie var areaid = '.area' + (Math.round(k / 10) + 1); $(areaid).append('<div class="painting"><img src="images/' + img + '" width="200" height="225" alt="' + title + '" /><br/><div class="title">' + title + '<br/>$' + price + '</div></div>') }); }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 javascript(不使用 jquery)将 HTML 表单数据发送到 JSON? - How do I send HTML form data to JSON using javascript (without jquery)? 如何将 JSON 文件数据显示到 HTML 表中(仅使用 JavaScript,而不是 jQuery) - How do I display JSON file data into HTML table (using JavaScript only, not jQuery) 如何使用javascript将json对象附加到html主体? (没有jQuery) - How to append json objects to html body using javascript? (No jQuery) 如何在JavaScript中将JSON数据附加到HTML? - How to append the JSON data to HTML in JavaScript? 如何使用 javascript 将 append 元素添加到空的 html 文件? - How do I append elements to an empty html file using javascript? 使用 Javascript(无 jquery)从 2 个 json 字符串追加数据 - 追加不是函数 - Using Javascript (no jquery) to append data from 2 json strings - append is not a function 如何使用 Javascript 将查询参数 append 作为我的 URL 的查询参数? - How do I append a query parameter to my URL using Javascript? 如何使用 jQuery Ajax 成功在我的 html 上打印邮递员返回的 JSON 数组? - How do I print my postman returned JSON array on my html using jQuery Ajax success? 如何使用Javascript而不是jQuery动态填充带有JSON数据的html元素? - How do I dynamically populate html elements with JSON Data with Javascript not jQuery? 为什么不能附加到JSON对象? (jQuery / Java语言) - Why can't I append to my JSON object? (jQuery/Javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM