Accessing Data in the Json File

2019-09-16 00:09发布

I am having hard time to access the particular json data. Here`s my json file

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

      "numProducts": "85"
    }, {
      "avgPrice": "60",

      "numProducts": "49"
    }, {
      "avgPrice": "59",

      "numProducts": "65"
    }

I want to take the value of avgPrice and numProducts of the unique values first corresponding to the merchant name. For example In the json data, First and last merchantName is same(i.e "merchantName" : "A") . So I want to take the value of Merchant A first, and Merchant B(if it is repeated I want to complete that first and then go for the another Merchant.

var mn = [];
$.each(returnedData.data, function (index, value) {
  if($.inArray(value.merchantName, mn) == -1) {
    mn.push(value.merchantName);
  }
});
 //all the merchants name stored in mn[]

function get_items_by_merchant(merchant_name) {
  var items = new Array();
  $.each(returnedData.data, function (index, item) {
    if(returnedData.merchantName == merchant_name)
      items.push(item);
  });
  return items;
}
var CB_items = [];
for(var i = 0; i < mn.length; i++) {
  CB_items[i] = get_items_by_merchant(mn[i]);
  $.each(CB_items, function (index, item) {
    var avgpricve = parseFloat(response.data[i].avgPrice);
    var numproducts = parseFloat(response.data[i].numProducts);
    datajson = {
      x: avgpricve,
      y: numproducts
    }
    result_data.push(datajson)
  });
}

response is the data in json file, I am getting it using $.getJSON . In the above code I want to

access merchant name line response.data[i].mn[i].avgPrice.. Since that I am unable to.. Is there any way that I can do?

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-16 00:18

In the function get_items_by_merchant change this

$.each(returnedData.data, function(index, item) {
                if (returnedData.merchantName == merchant_name) // There is not merchantName in returnedData.
                    items.push(item);
   });

to

 $.each(returnedData.data, function(index, item) {
            if (item.merchantName == merchant_name)
                items.push(item);
        });

Final Code with changes:-

Demo

var result_data = [];
var mn = [];
$.each(returnedData.data, function (index, value) {
    if ($.inArray(value.merchantName, mn) == -1) {
        mn.push(value.merchantName);
    }
});
//all the merchants name stored in mn[]

function get_items_by_merchant(merchant_name) {
    var items = new Array();
    $.each(returnedData.data, function (index, item) {
        if (item.merchantName == merchant_name) items.push(item);
    });
    return items;
}
var CB_items = [];
for (var i = 0; i < mn.length; i++) {

    CB_items[i] = get_items_by_merchant(mn[i]);
    $.each(CB_items[i], function (index, item) {
        var avgpricve = parseFloat(item.avgPrice);
        var numproducts = parseFloat(item.numProducts);
        datajson = {
            x: avgpricve,
            y: numproducts
        }
        result_data.push(datajson)
    });
    console.log(result_data)
查看更多
登录 后发表回答