显示 json 数组中的所有项
Display all items in a json array
我正在尝试使用json,我几乎拥有我需要的东西。我正在获取要显示的正确信息,但我必须将数组的每个项目传递到变量中,然后打印变量。我想显示每个数组中的所有项目。我正在使用的 json 数据来自发票应用程序 (www.curdbee.com(,我正在尝试为客户显示每张发票。我要显示的数据是每个订单项、订单项价格和总金额。这是我的代码:
$(document).ready(function() {
$.getJSON('https://nspirelab.curdbee.com/invoices.json?api_token=__token__', function(data){
$.each(data, function(index, item){
var total = item.invoice.total_billed;
var lineItemName1 = item.invoice.line_items[0].name_and_description;
var lineItemName2 = item.invoice.line_items[1].name_and_description;
var lineItemPrice1 = item.invoice.line_items[0].price;
var lineItemPrice2 = item.invoice.line_items[1].price;
$('#results').append('<div class="lineItem"><ul><li>' + lineItemName1 + lineItemPrice1 + '</li><li>' + lineItemName2 + lineItemPrice2 + '</li><li>' + total + '</li></ul></div>');
});
});
});
嵌套
循环(或者在jQuery中,嵌套$.each()
(将完成这项工作:
$.getJSON('https://nspirelab.curdbee.com/invoices.json?api_token=&client=104929', function(data){
$.each(data, function(index, item){
// create an empty ul (initially disconnected from the DOM)
var $ul = $("<ul/>");
// iterate over the line items
$.each(item.invoice.line_items, function(i, lineItem) {
// create an li element with the line details and append
// it to the ul
$ul.append( $("<li/>").html(lineItem.name_and_description + lineItem.price) );
});
// add the totals to the end of the ul
$ul.append( $("<li/>").html(item.invoice.total_billed) );
// create a new div, append the ul to it, and append the div to results
$('#results').append( $('<div class="lineItem"/>').append($ul) );
});
});
有
数量可笑的库可以使这样的事情变得更容易。 搜索"javascript 模板"以查看您缺少的内容,并了解您可以选择的各种库。
如果我正确理解了你的问题,我认为你正在寻找的是某种迭代机制,试着强调.js。
相关文章:
- 需要帮助设置json数组
- 如何使用 node.js 比较两个 json 数组
- 根据id将json数组组合为一个json数组
- 如何创建JSON数组
- 如何将package.json数组传递给grunt.js
- 访问json数组中的对象
- JSON数组数据返回Undefined
- 如何向JSON数组动态添加属性
- 如何从json数组中获取特定值
- 从多维嵌套json数组创建下拉列表
- 在ListView中添加JSON数组中存储的图像-ReactNative
- jquery在json数组中循环
- 将 JSON 数组解析为 JavaScript 数组
- 如何在php中读取没有任何键的JSON数组
- 如何在Jquery中迭代JSON数组
- 如何在cycle js中从JSON数组创建组件
- 如何使用jquery返回php-json数组对象
- JSON数组转换为JS对象数组
- 使用System从C#集合创建JSON数组.网状物剧本序列化
- 使用Underscore.js修改json数组中所选元素的更有效方法