JSON获取日期时的元素?

时间:2018-06-29 02:42:02

标签: jquery json ajax

我正在尝试使用GET请求并处理一些JSON。但是,结果在JSON中包含一个日期。我会做些什么-我可以选择之前的信息,但不能选择日期内的数据。我尝试“ 2018-06-28”无济于事。

"element_count" : 8,
  "near_earth_objects" : {
    "2018-06-28" : [ {
      "links" : {
        "self" : "https://api.nasa.gov/neo/rest/v1/neo/3251512?api_key=OFC1FIb06lwdyot0ZN3yRQAQe8pvhK0R1wCx5GAu"
      },
      "neo_reference_id" : "3251512",
      "name" : "(2004 RX10)",
      "nasa_jpl_url" : "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3251512",
      "absolute_magnitude_h" : 21.3,
      "estimated_diameter" : {
        "kilometers" : {
          "estimated_diameter_min" : 0.1460679643,
          "estimated_diameter_max" : 0.3266178974
        },
        "meters" : {
          "estimated_diameter_min" : 146.0679642714,
          "estimated_diameter_max" : 326.6178974458
        },
        "miles" : {
          "estimated_diameter_min" : 0.090762397,
          "estimated_diameter_max" : 0.2029508896
        },
        "feet" : {
          "estimated_diameter_min" : 479.2256199,
          "estimated_diameter_max" : 1071.581062656
        }
      },
      "is_potentially_hazardous_asteroid" : false,
      "close_approach_data" : [ {
        "close_approach_date" : "2018-06-28",
        "epoch_date_close_approach" : 1530169200000,
        "relative_velocity" : {
          "kilometers_per_second" : "13.0790754914",
          "kilometers_per_hour" : "47084.6717691282",
          "miles_per_hour" : "29256.5839667675"
        },
        "miss_distance" : {
          "astronomical" : "0.1504186394",
          "lunar" : "58.5128517151",
          "kilometers" : "22502308",
          "miles" : "13982286"
        },
        "orbiting_body" : "Earth"
      } ]
    },

这是我到目前为止尝试过的:

jQuery.getJSON("neo-current.json", function(data) {  
    $("#neo").html(data.element_count+" NEO(s)");
    var date = "2018-06-28";
    $.each(data, function(index, i) {
        console.log(i.near_earth_objects.date.name);
    });
});

2 个答案:

答案 0 :(得分:1)

应该是:

console.log(i.near_earth_objects[date].name);

答案 1 :(得分:0)

找到了解决方案。

jQuery(document).ready(function($) {
    var hazardous = '0';
    jQuery.getJSON("neo-current.json", function(data) {  
        $("#neo").html(data.element_count+" NEO(s)");
        var date = Object.keys(data.near_earth_objects)[0];
        $.each(data.near_earth_objects[date], function(index, i) {
            console.log(i.name+" "+i.is_potentially_hazardous_asteroid);
            if (i.is_potentially_hazardous_asteroid == true) {
                hazardous ++
            } else {
                //Do Nothing
            }
        });
        $("#hazardous-neo").html(hazardous+" NEO(S)");
    });
});