Accessing Data in the Json File

你离开我真会死。 提交于 2019-12-13 07:23:53

问题


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:


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)


来源:https://stackoverflow.com/questions/16504621/accessing-data-in-the-json-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!