这是一个doozie。
我有以下.json格式的文件:
"response": {
"tickets": [
{
"alias": "Lower Box 122",
"curr_price": 16.0,
"quantity": 2,
"seats": "17,18",
},
{
"alias": "Lower Box 122",
"curr_price": 8.0,
"quantity": 5,
"seats": "1,2,3,4,5",
},
{
"alias": "Lower Box 122",
"curr_price": 14.0,
"quantity": 7,
"seats": "6,7,8,9,10,11,12",
},
{
"alias": "Lower Box 123",
"curr_price": 16.0,
"quantity": 2,
"seats": "17,18",
},
{
"alias": "Lower Box 123",
"curr_price": 8.0,
"quantity": 5,
"seats": "1,2,3,4,5",
},
{
"alias": "Lower Box 123",
"curr_price": 14.0,
"quantity": 7,
"seats": "6,7,8,9,10,11,12",
}
]
}
我正在尝试获得此输出:
Lower Box 122,
quantity: 15,
seats: 1,2,3,4,5,6,7,8,9,10,11,12,17,18,
price: 8-16
Lower Box 123,
quantity: 15,
seats: 1,2,3,4,5,6,7,8,9,10,11,12,17,18,
price: 8-16
不幸的是,无法重新格式化原始JSON文件。我必须处理它目前的格式。
我不一定需要再次使用.json格式。将文本吐出到页面就可以了。任何人都可以指向一个实用程序吗?还是一个可以解决这个问题的脚本?
难度:仅限javascript。
我要感谢所有有答案的人 - 希望这对其他人来说会派上用场。
答案 0 :(得分:2)
<script type="text/javascript">
$(document).ready(function () {
var response = {"tickets":[{"alias":"Lower Box 122","curr_price":16.0,"quantity":2,"seats":"17,18",},{"alias":"Lower Box 122","curr_price":8.0,"quantity":5,"seats":"1,2,3,4,5",},{"alias":"Lower Box 122","curr_price":14.0,"quantity":7,"seats":"6,7,8,9,10,11,12",},{"alias":"Lower Box 123","curr_price":16.0,"quantity":2,"seats":"17,18",},{"alias":"Lower Box 123","curr_price":8.0,"quantity":5,"seats":"1,2,3,4,5",},{"alias":"Lower Box 123","curr_price":14.0,"quantity":7,"seats":"6,7,8,9,10,11,12",}]};
var seats = new Array();
var alias = "";
var quantity;
var min_price = 999999;
var max_price = -1;
$.each(response.tickets, function(key, value){
if(value.alias != alias)
{
if(alias != "")
{
seats = seats.sort(function(a,b){ return parseInt(a) > parseInt(b); });
alert(alias + "\ncurr_price: " + min_price + "-" + max_price + "\nquantity: " + quantity + "\nseats: " + seats);
}
alias = value.alias;
quantity = 0;
min_price = 999999;
max_price = -1;
seats = new Array();
}
if(value.curr_price < min_price)
{
min_price = value.curr_price;
}
if(value.curr_price > max_price)
{
max_price = value.curr_price;
}
$.each(value.seats.split(","), function(key, value){
if($.inArray(value, seats) < 0)
{
seats.push(parseInt(value));
}
});
quantity += parseInt(value.quantity);
});
//Call again for last one
seats = seats.sort(function(a,b){ return parseInt(a) > parseInt(b); });
alert(alias + "\ncurr_price: " + min_price + "-" + max_price + "\nquantity: " + quantity + "\nseats: " + seats);
});
</script>
答案 1 :(得分:1)
如果您愿意加入Underscore.js,可以使用reduce
/ lfold
方法。
我试了一下,但我怀疑它可以稍微加强:
_.reduce(x.response.tickets, function(memo, obj){
memo[obj.alias] = memo[obj.alias] || {quantity:0, seats:[]};
memo[obj.alias].maxPrice =
Math.max(memo[obj.alias].maxPrice || obj.curr_price, obj.curr_price);
memo[obj.alias].minPrice =
Math.min(memo[obj.alias].minPrice || obj.curr_price, obj.curr_price);
memo[obj.alias].quantity += obj.quantity;
memo[obj.alias].seats =
memo[obj.alias].seats.concat(_.map(obj.seats.split(","), function(v){
return parseInt(v,10);
}));
memo[obj.alias].seats.sort(function(a,b){return a-b;});
return memo;
}, {})
让你:
{
"Lower Box 122": {
"quantity": 14,
"seats": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 18],
"maxPrice": 16,
"minPrice": 8
},
"Lower Box 123": {
"quantity": 14,
"seats": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 18],
"maxPrice": 16,
"minPrice": 8
}
}
此外,如果您希望结果是一个对象数组而不是哈希值,您可以像这样进行转换(假设var k
是上面折叠的结果):
_.keys(k).map(function(alias){
k[alias].alias = alias;
return k[alias];
})
答案 2 :(得分:0)
以下内容将以正确的格式为您吐出:
var obj = { /* your JSON goes here */ };
for (var i=0; i<obj.response.tickets.length; i++) {
document.write(obj.response.tickets[i].alias + ",<br/>");
document.write("quantity: " + obj.response.tickets[i].quantity + ",<br/>");
document.write("seats: " + obj.response.tickets[i].seats + ",<br/>");
document.write("price: " + obj.response.tickets[i].curr_price + "<br/><br/>");
}
答案 3 :(得分:0)
我不确定你是如何解析那个示例输出的,因为你用逗号分隔字段,但你的一个值中有逗号,那里有歧义。
假设您的JSON被解析为名为data
的对象,您可以使用jQuery:
jQuery.map(data.response.tickets, function(elt) {
var fields = [];
fields.push(elt.alias);
fields.push("quantity:" + String(elt.quantity));
fields.push("price:" + String(elt.curr_price));
fields.push("seats:" + elt.seats);
return fields.join(",\r\n");
}).join("\r\n\r\n");
答案 4 :(得分:0)
合并这些故障单类型所需的一些工作。
(function($) {
$(document).ready(function() {
var responses = {};
$.getJSON('responsefile.json', function(data) {
$.each(data.response.tickets, function(index, cTicket) {
var cSeats = cTicket.seats.split(/,/);
if (responses[cTicket.alias] == undefined) {
responses[cTicket.alias] = { alias: cTicket.alias,
price: { min: cTicket.curr_price, max: cTicket.curr_price },
quantity: cTicket.quantity,
seats: cSeats };
} else {
var cResponse = responses[cTicket.alias];
$.each(cSeats, function(i, cSeatNumber) {
if ($.inArray(cSeatNumber, cResponse.seats) == -1) {
$.merge(cResponse.seats, [cSeatNumber]);
}
});
cResponse.seats.sort(function(a, b) {
return parseInt(a) - parseInt(b);
});
cResponse.quantity = responses[cTicket.alias].seats.length;
cResponse.price.min = (cTicket.curr_price < cResponse.price.min ? cTicket.curr_price : cResponse.price.min);
cResponse.price.max = (cTicket.curr_price > cResponse.price.max ? cTicket.curr_price : cResponse.price.max);
responses[cTicket.alias] = cResponse;
}
});
var responseSet = [];
$.each(responses, function(index, cResponse) {
responseSet.push(cResponse);
});
responseSet.sort(function(rA, rB) {
return (rA.alias < rB.alias ? -1 : (rA.alias > rB.alias ? 1 : 0));
});
processResponses(responseSet);
});
});
})(jQuery);
当您请求一种写出响应的方法时,我稍微修改了上面的代码,以便在退出时调用函数。只需在下面调整此功能即可指向正确的位置。
(function($) {
function processResponses(responses) {
var responseText = '';
var csv = "alias,quantity,seats,price\n";
$.each(responses, function(index, cResponse) {
responseText = responseText
+ cResponse.alias + ",\n"
+ "quantity: " + cResponse.quantity + ",\n"
+ "seats: " + cResponse.seats.join(',') + ",\n"
+ "price: " + cResponse.price.min + (cResponse.price.min == cResponse.price.max ? '' : '-' + cResponse.price.max) + "\n";
csv = csv
+ cResponse.alias + ","
+ cResponse.quantity + ","
+ cResponse.seats.join(' ') + ","
+ cResponse.price.min + (cResponse.price.min == cResponse.price.max ? '' : '-' + cResponse.price.max) + "\n";
});
$('#text-output-location').text(responseText);
$('#csv-output-location').text(csv);
}
})(jQuery);
请注意,需要在CSV输出中空间分隔座位值,因为逗号分隔它们需要大量转义并降低清晰度。
NB。演示版本有一些修改,允许AJAX在jsfiddle.net上进行,这需要特殊处理,也需要对输出进行轻微更改以添加换行符。