访问Json文件中的数据 [英] Accessing Data in the Json File

查看:112
本文介绍了访问Json文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难访问特定的json数据.这是我的json文件

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"
    }

我想采用首先与商家名称相对应的唯一值avgPrice和numProducts的值.例如,在json数据中,第一个和最后一个商人名相同(即"merchantName":"A").因此,我想先评估商人A的价值,然后再评估商人B的价值(如果重复此操作,我首先要完成该价值,然后再选择另一个商人.

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

响应是json文件中的数据,我正在使用$ .getJSON来获取它.在上面的代码中,我想

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

访问商家名称行response.data [i] .mn [i] .avgPrice ..既然这样,我就无法..有什么办法可以做?

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

推荐答案

在函数get_items_by_merchant中对此进行更改

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

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

更改的最终代码:-

演示

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)

这篇关于访问Json文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆