2

如何使用 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
        }
    ]
}
4

2 回答 2

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);
于 2013-05-10T13:06:58.213 回答
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;
    }
}

小提琴

于 2013-05-10T13:04:25.150 回答