2011-02-16 124 views
2

这是一个doozie。试图找出如何重新格式化JSON响应并重新格式化数据

我有以下以.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

的Javascript与jQuery推测,因为你标记jQuery的。 – Orbling 2011-02-16 00:05:19

+0

你如何获得这个文件到浏览器中,一个'.getJSON()`调用? – Orbling 2011-02-16 00:05:46

+0

是。 getJSON - 我可以使用getScript也 – jake 2011-02-16 00:31:35

回答

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输出的座位值,用逗号分隔它们将需要大量逸出,降低清晰度。

演示:http://jsfiddle.net/p5QUb/

NB。演示版本有一些修改以允许AJAX在jsfiddle.net上发生,这需要特殊处理,并且输出的细微更改以添加换行符。

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/>"); 
} 
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"); 
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

如果您愿意包括Underscore.js您可以使用reduce/lfold方法。 我给它一个镜头,但我怀疑它可能neatened了一点:

_.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]; 
})