如何使用Groovy脚本比较JSON响应中的多个对象

时间:2019-06-11 15:14:23

标签: json groovy

我正在使用提供给我的API,以学习用于声明JSON响应的常规脚本。

API是

  

https://api.nasa.gov/neo/rest/v1/feed?start_date=2019-06-10&end_date=2019-06-16&api_key= *************

您可以从此处获取密钥:https://api.nasa.gov/index.html#apply-for-an-api-key

这是json响应的一小部分:

    "element_count": 69,
       "near_earth_objects":    {
          "2019-05-10":       [
                      {
                "links": {"self": "http://www.neowsapp.com/rest/v1/neo/3842596?api_key=******"},
                "id": "3842596",
                "neo_reference_id": "3842596",
                "name": "(2019 KM2)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3842596",
                "absolute_magnitude_h": 25.762,
                "estimated_diameter":             {
                   "kilometers":                {
                      "estimated_diameter_min": 0.0187134981,
                      "estimated_diameter_max": 0.0418446538
                   },
                   "meters":                {
                      "estimated_diameter_min": 18.7134980647,
                      "estimated_diameter_max": 41.8446537694
                   },
                   "miles":                {
                      "estimated_diameter_min": 0.011628025,
                      "estimated_diameter_max": 0.0260010544
                   },
                   "feet":                {
                      "estimated_diameter_min": 61.3959929905,
                      "estimated_diameter_max": 137.2856138729
                   }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [            {
                   "close_approach_date": "2019-05-10",
                   "close_approach_date_full": "2019-May-10 19:38",
                   "epoch_date_close_approach": 1557517080000,
                   "relative_velocity":                {
                      "kilometers_per_second": "2.5687395339",
                      "kilometers_per_hour": "9247.4623220529",
                      "miles_per_hour": "5746.0134633889"
                   },
                   "miss_distance":                {
                      "astronomical": "0.0830780649",
                      "lunar": "32.3173672461",
                      "kilometers": "12428301.552761763",
                      "miles": "7722588.4836998094"
                   },
                   "orbiting_body": "Earth"
                }],
                "is_sentry_object": false
             },

当天文学的每个对象(在这种情况下为每个流星)与上述API的天文miss_distance大于0.05时,如何断言is_potentially_hazardous_asteroid是假的

我知道如何使用JsonSlurper解析响应,但是我不知道如何在每个对象中进行比较。抱歉,我是groovy和json的新手。

我尝试了以下方法,但还是没有运气

    import groovy.json.JsonSlurper
    def ResponseMessage = messageExchange.response.responseContent
    def obj = new JsonSlurper().parseText(ResponseMessage)
    obj.near_earth_objects.each{
        date,objects->objects.each{
            Object neo = it
            neo.close_approach_data.each{
                if((it.miss_distance.astronomical as Double)>0.05){
                    assert (neo.is_potentially_hazardous_asteroid == false)
                }
            }
        }
    }

我得到的错误:

   assert (neo.is_potentially_hazardous_asteroid == false) | | | | true                                                                 
   false [absolute_magnitude_h:21.1, close_approach_data: 
   [[close_approach_date:2019-05-10, close_approach_date_full:2019-May-10 
   16:45, epoch_date_close_approach:1557506700000, miss_distance: 
   [astronomical:0.2259243576, kilometers:33797802.678078312, 
   lunar:87.8845751064, miles:21000980.7557402256], orbiting_body:Earth, 
   relative_velocity:[kilometers_per_hour:61066.4765518526, 
   kilometers_per_second:16.9629101533, miles_per_hour:37944.3337218998]]], 
   estimated_diameter:[feet:[estimated_diameter_max:1174.9652706022, 
   estimated_diameter_min:525.4604432536], kilometers: 
   [estimated_diameter_max:0.358129403, estimated_diameter_min:0.160160338], 
   meters:[estimated_diameter_max:358.1294030194, 
   estimated_diameter_min:160.1603379786], miles: 
   [estimated_diameter_max:0.2225312253, 
   estimated_diameter_min:0.0995189894]], id:3258077, 
   is_potentially_hazardous_asteroid:true, is_sentry_object:false, links: 
   [self:http://www.neowsapp.com/rest/v1/neo/3258077?api_key=****], name: 
   (2004 UU1), nasa_jpl_url:http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3258077, 
   neo_reference_id:3258077]

我应该提到我是在soapui上通过添加脚本断言来实现的

1 个答案:

答案 0 :(得分:0)

据我所知,您的代码可以正常工作...

我对您的脚本进行了一些调整,并得到了相同的结果(有效证明)...

obj.near_earth_objects.each { date, objects ->
  objects.each { neo ->
    if (neo.close_approach_data.find { it.miss_distance.astronomical.toDouble() > 0.05 }) {
      assert !neo.is_potentially_hazardous_asteroid
    }
  }
}