从json数据中获取唯一值

时间:2013-05-10 12:51:50

标签: javascript jquery json

如何使用jquery获取唯一商家名称以及相应的avgprice和numproducts?我一直很难分离json数据。请帮帮我们..我正在使用$ .getJSON解析这个json文件。我们正在创建图表,我们需要在每个商家名称后使用分隔符。所以这里的avgprice和numProducts是x和y轴的值。所以,如果我使用这样的东西

$.getJSON("mock/Insight72.json", function(returnedData) {
        dataLength = returnedData.data.length;
console.log(returnedData)
response = returnedData;
        var x = new Array();
        var y = new Array();
        var mytext, f;

        for ( i = 0; i < dataLength; i++) {

            x[i] = returnedData.data[i].avgPrice;
            y[i] = returnedData.data[i].numProducts;

x轴和y轴以相同的顺序存储。但我需要解析它应该采取与商家名称相对应的avgprice和numProducts。希望这澄清..

 {
    "id" : "72",
    "title" : "Item Category Product Level Average Price Comparison",
    "xLabel" : null,
    "yLabel" : "Average Price",
    "zLabel" : null,

    "data" : [
        {
            "avgPrice" : 10,
            "categoryID" : "152b7934dd7e4fbeac56f825b5deaebd",
            "categoryName" : "Coffee Makers",
            "merchantID" : "99a8cd5687d245f8bff2152fec710973",
            "merchantName" : "Crate & Barrel",
            "numProducts" : 400
        },
        {
            "avgPrice" : 20,
            "categoryID" : "152b7934dd7e4fbeac56f825b5deaebd",
            "categoryName" : "Coffee Makers",
            "merchantID" : "f5b2c736eace488a859fb6c56f366522",
            "merchantName" : "Williams-Sonoma",
            "numProducts" : 500 
        },
        {
            "avgPrice" :30,
            "categoryID" : "152b7934dd7e4fbeac56f825b5deaebd",
            "categoryName" : "Coffee Makers",
            "merchantID" : "6ee163f4f236466289fae97bb40351b7",
            "merchantName" : "Amazon",
            "numProducts" : 38
        },
        {
            "avgPrice" : 40,
            "categoryID" : "50215adc9ef64f91b4f4898fda7cf0b5",
            "categoryName" : "Dishwashers",
            "merchantID" : "99a8cd5687d245f8bff2152fec710973",
            "merchantName" : "Crate & Barrel",
            "numProducts" : 300 
        },
        {
            "avgPrice" : 50,
            "categoryID" : "50215adc9ef64f91b4f4898fda7cf0b5",
            "categoryName" : "Dishwashers",
            "merchantID" : "f5b2c736eace488a859fb6c56f366522",
            "merchantName" : "Williams-Sonoma",
            "numProducts" : 320
        },
        {
            "avgPrice" : 60,
            "categoryID" : "50215adc9ef64f91b4f4898fda7cf0b5",
            "categoryName" : "Dishwashers",
            "merchantID" : "6ee163f4f236466289fae97bb40351b7",
            "merchantName" : "Amazon",
            "numProducts" : 350
        }
    ]
}

2 个答案:

答案 0 :(得分:2)

这是工作小提琴:http://jsfiddle.net/Esv6f/

/* get all items by merchant*/
function get_items_by_merchant(merchant_name) {
    var items = new Array();
    $.each(json.data, function(index, item) {
        if (item.merchantName == merchant_name) 
            items.push(item);
    });

    return items;
}

/* get items of merchant "Amazon" */
var amazon_items = get_items_by_merchant("Amazon");

/* count the total num of products */ 
var amazon_num_products_total = 0;

/* loop items */
$.each(amazon_items, function(index, item) {
    amazon_num_products_total += item.numProducts; // add numProducts 

    /* alert the avgPrice and numProducts of the CURRENT item */
    alert("avgPrice: " + item.avgPrice + " numProducts: " + item.numProducts);
});

/* alert the total num of products of amazon */ 
alert("Amazon numProducts total: " + amazon_num_products_total);

答案 1 :(得分:0)

此函数将返回它找到的第一个匹配项:

function getMerchantName(data, avgPrice, numProducts) {
    for(x in a.data) {
        if(a.data[x].avgPrice === avgPrice && a.data[x].numProducts === numProducts)
            return a.data[x].merchantName;
    }
}

Fiddle