StrongLoop/LoopBack : 在 Rest 连接器数据源模板上获取完整 jSON

StrongLoop/LoopBack : get full jSON on Rest connector datasource template

我正在尝试从 google 地理编码 API 获得完整的 jSON 响应,以便我可以测试结果 "status"。

来自文档:

responsePath: an optional JSONPath applied to the HTTP body. See https://github.com/s3u/JSONPath for syntax of JSON paths.

所以我使用了 "responsePath":"$..*" 但它得到的响应全都搞砸了。

示例:

        [
           [
              {
                 "address_components":[
                    {
                       "long_name":"Caracas",
                       "short_name":"CCS",
                       "types":[
                          "locality",
                          "political"
                       ]
                    },
                    {
                       "long_name":"Libertador",
                       "short_name":"Libertador",
                       "types":[
                          "administrative_area_level_2",
                          "political"
                       ]
                    },
                    {
                       "long_name":"Capital District",
                       "short_name":"Dto. Capital",
                       "types":[
                          "administrative_area_level_1",
                          "political"
                       ]
                    },
                    {
                       "long_name":"Venezuela",
                       "short_name":"VE",
                       "types":[
                          "country",
                          "political"
                       ]
                    }
                 ],
                 "formatted_address":"Caracas, Venezuela",
                 "geometry":{
                    "bounds":{
                       "northeast":{
                          "lat":10.5401335,
                          "lng":-66.7179533
                       },
                       "southwest":{
                          "lat":10.39665,
                          "lng":-67.0627784
                       }
                    },
                    "location":{
                       "lat":10.4696404,
                       "lng":-66.8037185
                    },
                    "location_type":"APPROXIMATE",
                    "viewport":{
                       "northeast":{
                          "lat":10.5401335,
                          "lng":-66.7179533
                       },
                       "southwest":{
                          "lat":10.39665,
                          "lng":-67.0627784
                       }
                    }
                 },
                 "place_id":"ChIJB0iCza1YKowRg4SZCq4u3ZM",
                 "types":[
                    "locality",
                    "political"
                 ]
              },
              {
                 "address_components":[
                    {
                       "long_name":"Venezuela",
                       "short_name":"VE",
                       "types":[
                          "country",
                          "political"
                       ]
                    }
                 ],
                 "formatted_address":"Venezuela",
                 "geometry":{
                    "bounds":{
                       "northeast":{
                          "lat":12.4866941,
                          "lng":-59.805666
                       },
                       "southwest":{
                          "lat":0.647529,
                          "lng":-73.351558
                       }
                    },
                    "location":{
                       "lat":6.42375,
                       "lng":-66.58973
                    },
                    "location_type":"APPROXIMATE",
                    "viewport":{
                       "northeast":{
                          "lat":14.470409,
                          "lng":-59.80378
                       },
                       "southwest":{
                          "lat":-4.1438179,
                          "lng":-79.0255611
                       }
                    }
                 },
                 "place_id":"ChIJAdjLNstTKIwR003VfFjyoNw",
                 "types":[
                    "country",
                    "political"
                 ]
              }
           ],
           "OK",
           {
              "address_components":[
                 {
                    "long_name":"Caracas",
                    "short_name":"CCS",
                    "types":[
                       "locality",
                       "political"
                    ]
                 },
                 {
                    "long_name":"Libertador",
                    "short_name":"Libertador",
                    "types":[
                       "administrative_area_level_2",
                       "political"
                    ]
                 },
                 {
                    "long_name":"Capital District",
                    "short_name":"Dto. Capital",
                    "types":[
                       "administrative_area_level_1",
                       "political"
                    ]
                 },
                 {
                    "long_name":"Venezuela",
                    "short_name":"VE",
                    "types":[
                       "country",
                       "political"
                    ]
                 }
              ],
              "formatted_address":"Caracas, Venezuela",
              "geometry":{
                 "bounds":{
                    "northeast":{
                       "lat":10.5401335,
                       "lng":-66.7179533
                    },
                    "southwest":{
                       "lat":10.39665,
                       "lng":-67.0627784
                    }
                 },
                 "location":{
                    "lat":10.4696404,
                    "lng":-66.8037185
                 },
                 "location_type":"APPROXIMATE",
                 "viewport":{
                    "northeast":{
                       "lat":10.5401335,
                       "lng":-66.7179533
                    },
                    "southwest":{
                       "lat":10.39665,
                       "lng":-67.0627784
                    }
                 }
              },
              "place_id":"ChIJB0iCza1YKowRg4SZCq4u3ZM",
              "types":[
                 "locality",
                 "political"
              ]
           },
           {
              "address_components":[
                 {
                    "long_name":"Venezuela",
                    "short_name":"VE",
                    "types":[
                       "country",
                       "political"
                    ]
                 }
              ],
              "formatted_address":"Venezuela",
              "geometry":{
                 "bounds":{
                    "northeast":{
                       "lat":12.4866941,
                       "lng":-59.805666
                    },
                    "southwest":{
                       "lat":0.647529,
                       "lng":-73.351558
                    }
                 },
                 "location":{
                    "lat":6.42375,
                    "lng":-66.58973
                 },
                 "location_type":"APPROXIMATE",
                 "viewport":{
                    "northeast":{
                       "lat":14.470409,
                       "lng":-59.80378
                    },
                    "southwest":{
                       "lat":-4.1438179,
                       "lng":-79.0255611
                    }
                 }
              },
              "place_id":"ChIJAdjLNstTKIwR003VfFjyoNw",
              "types":[
                 "country",
                 "political"
              ]
           },
           [
              {
                 "long_name":"Caracas",
                 "short_name":"CCS",
                 "types":[
                    "locality",
                    "political"
                 ]
              },
              {
                 "long_name":"Libertador",
                 "short_name":"Libertador",
                 "types":[
                    "administrative_area_level_2",
                    "political"
                 ]
              },
              {
                 "long_name":"Capital District",
                 "short_name":"Dto. Capital",
                 "types":[
                    "administrative_area_level_1",
                    "political"
                 ]
              },
              {
                 "long_name":"Venezuela",
                 "short_name":"VE",
                 "types":[
                    "country",
                    "political"
                 ]
              }
           ],
           "Caracas, Venezuela",
           {
              "bounds":{
                 "northeast":{
                    "lat":10.5401335,
                    "lng":-66.7179533
                 },
                 "southwest":{
                    "lat":10.39665,
                    "lng":-67.0627784
                 }
              },
              "location":{
                 "lat":10.4696404,
                 "lng":-66.8037185
              },
              "location_type":"APPROXIMATE",
              "viewport":{
                 "northeast":{
                    "lat":10.5401335,
                    "lng":-66.7179533
                 },
                 "southwest":{
                    "lat":10.39665,
                    "lng":-67.0627784
                 }
              }
           },
           "ChIJB0iCza1YKowRg4SZCq4u3ZM",
           [
              "locality",
              "political"
           ],
           {
              "long_name":"Caracas",
              "short_name":"CCS",
              "types":[
                 "locality",
                 "political"
              ]
           },
           {
              "long_name":"Libertador",
              "short_name":"Libertador",
              "types":[
                 "administrative_area_level_2",
                 "political"
              ]
           },
           {
              "long_name":"Capital District",
              "short_name":"Dto. Capital",
              "types":[
                 "administrative_area_level_1",
                 "political"
              ]
           },
           {
              "long_name":"Venezuela",
              "short_name":"VE",
              "types":[
                 "country",
                 "political"
              ]
           },
           "Caracas",
           "CCS",
           [
              "locality",
              "political"
           ],
           "locality",
           "political",
           "Libertador",
           "Libertador",
           [
              "administrative_area_level_2",
              "political"
           ],
           "administrative_area_level_2",
           "political",
           "Capital District",
           "Dto. Capital",
           [
              "administrative_area_level_1",
              "political"
           ],
           "administrative_area_level_1",
           "political",
           "Venezuela",
           "VE",
           [
              "country",
              "political"
           ],
           "country",
           "political",
           {
              "northeast":{
                 "lat":10.5401335,
                 "lng":-66.7179533
              },
              "southwest":{
                 "lat":10.39665,
                 "lng":-67.0627784
              }
           },
           {
              "lat":10.4696404,
              "lng":-66.8037185
           },
           "APPROXIMATE",
           {
              "northeast":{
                 "lat":10.5401335,
                 "lng":-66.7179533
              },
              "southwest":{
                 "lat":10.39665,
                 "lng":-67.0627784
              }
           },
           {
              "lat":10.5401335,
              "lng":-66.7179533
           },
           {
              "lat":10.39665,
              "lng":-67.0627784
           },
           10.5401335,
           -66.7179533,
           10.39665,
           -67.0627784,
           10.4696404,
           -66.8037185,
           {
              "lat":10.5401335,
              "lng":-66.7179533
           },
           {
              "lat":10.39665,
              "lng":-67.0627784
           },
           10.5401335,
           -66.7179533,
           10.39665,
           -67.0627784,
           "locality",
           "political",
           [
              {
                 "long_name":"Venezuela",
                 "short_name":"VE",
                 "types":[
                    "country",
                    "political"
                 ]
              }
           ],
           "Venezuela",
           {
              "bounds":{
                 "northeast":{
                    "lat":12.4866941,
                    "lng":-59.805666
                 },
                 "southwest":{
                    "lat":0.647529,
                    "lng":-73.351558
                 }
              },
              "location":{
                 "lat":6.42375,
                 "lng":-66.58973
              },
              "location_type":"APPROXIMATE",
              "viewport":{
                 "northeast":{
                    "lat":14.470409,
                    "lng":-59.80378
                 },
                 "southwest":{
                    "lat":-4.1438179,
                    "lng":-79.0255611
                 }
              }
           },
           "ChIJAdjLNstTKIwR003VfFjyoNw",
           [
              "country",
              "political"
           ],
           {
              "long_name":"Venezuela",
              "short_name":"VE",
              "types":[
                 "country",
                 "political"
              ]
           },
           "Venezuela",
           "VE",
           [
              "country",
              "political"
           ],
           "country",
           "political",
           {
              "northeast":{
                 "lat":12.4866941,
                 "lng":-59.805666
              },
              "southwest":{
                 "lat":0.647529,
                 "lng":-73.351558
              }
           },
           {
              "lat":6.42375,
              "lng":-66.58973
           },
           "APPROXIMATE",
           {
              "northeast":{
                 "lat":14.470409,
                 "lng":-59.80378
              },
              "southwest":{
                 "lat":-4.1438179,
                 "lng":-79.0255611
              }
           },
           {
              "lat":12.4866941,
              "lng":-59.805666
           },
           {
              "lat":0.647529,
              "lng":-73.351558
           },
           12.4866941,
           -59.805666,
           0.647529,
           -73.351558,
           6.42375,
           -66.58973,
           {
              "lat":14.470409,
              "lng":-59.80378
           },
           {
              "lat":-4.1438179,
              "lng":-79.0255611
           },
           14.470409,
           -59.80378,
           -4.1438179,
           -79.0255611,
           "country",
           "political"
        ]

好了,看来清楚了。 'Messed up' 响应是应用 JSON 路径表达式的结果,其结果是数组,而不是 JSON 对象。您可以在此处查看一些示例:https://github.com/s3u/JSONPath/blob/master/test/test.examples.js

因此,如果您只需要状态,您可以使用此过滤器:"responsePath": "$.status"。否则,您可以完全删除 responsePath 并获得原始 JSON 对象。