如何在此JSON文件中引用此数据?

时间:2022-11-27 19:36:37

I am using d3.js's "d3.json(); function to reach out and fetch some data from a REST API.

我正在使用d3.js的“d3.json();函数来伸出并从REST API中获取一些数据。

The data comes back as this:

数据回复如下:

{
"offset" : 0,
"rows": [
{ "_id":
    { "$oid" : "1234567" }, 
      "mockupData" : [ { 
          "Analysis" : "Test", 
          "Description" : "The Test indicates...", 
          "Data" : [
             { "Category" : "A", 
               "Statistic" : 0.15, 
               "Value" : 0.95 }, 
             { "Category" : "B", 
               "Statistic" : 0.65, 
               "Value" : 0.85 }, 
             ] } ] }
           ],
  "total_rows" : 1 ,
  "query" : {} ,
  "millis" : 0
}

I am having an extremely difficult time drilling down in to the json to get what I want.

我正在非常艰难地钻进json以获得我想要的东西。

I'm attempting to set it up like this:

我试图像这样设置它:

function generateChart(){
    var chart;
    var Category = [{key:"Stuff", values:[]}];
    d3.json('http://url_that_returns_json/', function(error,data{
       for(var key in data._SOMETHING_){
          switch(key){
             case "A":
                 Category[0] ["values"].push({"label":"Statistic","value""data.Category[key]});
... // same for B
})

// more graph logic

I appear to be missing some entire fragment of knowledge on this. Guidance? Help?

我似乎缺少一些关于此的知识片段。指导?帮帮我?

Thanks in advance.

提前致谢。

1 个解决方案

#1


2  

Here is a fiddle I have implemented : https://jsfiddle.net/thatOneGuy/486mwb86/1/

这是我实施的小提琴:https://jsfiddle.net/thatOneGuy/486mwb86/1/

First off, I set the data as a variable so I can use it later. So :

首先,我将数据设置为变量,以便稍后使用。所以:

var data = {
  "offset": 0,
  "rows": [{
    "_id": {
      "$oid": "1234567"
    }, ... //and so on

This is exactly the same as you using :

这与您使用的完全相同:

d3.json('http://url_that_returns_json/', function(error,data{

Both data variables are the same.

两个数据变量都是相同的。

Then I got to the point you wanted to check, i.e the categories in the data attribute, like so :

然后我到了你要检查的点,即数据属性中的类别,如下所示:

var thisDataSet = data.rows[0].mockupData[0].Data;

As this is an array and not an object, I used a for loop to loop through :

由于这是一个数组而不是一个对象,我使用for循环来遍历:

for (var i = 0; i < thisDataSet.length; i++) {

And, in my opinion, you didn't need a switch statement as it looks like you just wanted to populate Category.values with the different categories. So I just pushed the current value of the category in the dataset to this Category.values :

而且,在我看来,你不需要switch语句,因为看起来你只想用不同的类别填充Category.values。所以我只是将数据集中类别的当前值推送到此Category.values:

 Category[0]["values"].push({
      "label": "Statistic",
      "value": thisDataSet[i].Category //push category here
    });  

And that's it. Check the console log for output. Should work fine. Full function :

就是这样。检查控制台日志以获取输出。应该工作正常。全功能:

function generateChart() {
  var chart;
  var Category = [{
    key: "Stuff",
    values: []
  }]; 
  console.log(data.rows[0].mockupData[0].Data)
  var thisDataSet = data.rows[0].mockupData[0].Data;
  for (var i = 0; i < thisDataSet.length; i++) {
    //for (var key in data.rows[0].mockupData[0].Data) {
    console.log(thisDataSet[i]) 
    Category[0]["values"].push({
      "label": "Statistic",
      "value": thisDataSet[i].Category
    });  
  }
  console.log(Category) 
}

#1


2  

Here is a fiddle I have implemented : https://jsfiddle.net/thatOneGuy/486mwb86/1/

这是我实施的小提琴:https://jsfiddle.net/thatOneGuy/486mwb86/1/

First off, I set the data as a variable so I can use it later. So :

首先,我将数据设置为变量,以便稍后使用。所以:

var data = {
  "offset": 0,
  "rows": [{
    "_id": {
      "$oid": "1234567"
    }, ... //and so on

This is exactly the same as you using :

这与您使用的完全相同:

d3.json('http://url_that_returns_json/', function(error,data{

Both data variables are the same.

两个数据变量都是相同的。

Then I got to the point you wanted to check, i.e the categories in the data attribute, like so :

然后我到了你要检查的点,即数据属性中的类别,如下所示:

var thisDataSet = data.rows[0].mockupData[0].Data;

As this is an array and not an object, I used a for loop to loop through :

由于这是一个数组而不是一个对象,我使用for循环来遍历:

for (var i = 0; i < thisDataSet.length; i++) {

And, in my opinion, you didn't need a switch statement as it looks like you just wanted to populate Category.values with the different categories. So I just pushed the current value of the category in the dataset to this Category.values :

而且,在我看来,你不需要switch语句,因为看起来你只想用不同的类别填充Category.values。所以我只是将数据集中类别的当前值推送到此Category.values:

 Category[0]["values"].push({
      "label": "Statistic",
      "value": thisDataSet[i].Category //push category here
    });  

And that's it. Check the console log for output. Should work fine. Full function :

就是这样。检查控制台日志以获取输出。应该工作正常。全功能:

function generateChart() {
  var chart;
  var Category = [{
    key: "Stuff",
    values: []
  }]; 
  console.log(data.rows[0].mockupData[0].Data)
  var thisDataSet = data.rows[0].mockupData[0].Data;
  for (var i = 0; i < thisDataSet.length; i++) {
    //for (var key in data.rows[0].mockupData[0].Data) {
    console.log(thisDataSet[i]) 
    Category[0]["values"].push({
      "label": "Statistic",
      "value": thisDataSet[i].Category
    });  
  }
  console.log(Category) 
}