【问题标题】:StrongLoop/LoopBack : get full jSON on Rest connector datasource templateStrongLoop/LoopBack :在 Rest 连接器数据源模板上获取完整的 JSON
【发布时间】:2015-06-23 04:30:23
【问题描述】:

我正在尝试从谷歌地理编码 API 获取完整的 JSON 响应,以便测试结果“状态”。

来自文档:

responsePath:应用于 HTTP 正文的可选 JSONPath。看 https://github.com/s3u/JSONPath 获取 JSON 路径的语法。

所以我使用了 "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"
        ]

【问题讨论】:

  • 你能举一个'搞砸'响应的例子吗?您是否尝试检查通过 rest-connector 获得的响应是​​否与您在此处使用相同的 JSON 和 JSONPath 查询获得的结果不同:jsonpath.curiousconcept.com
  • 我已经编辑了这个问题。在该链接中测试我需要的表达式是“$”,但在强循环中它返回一个空的 []
  • "$*" 在 strongloop 上也给了我一个空的 []

标签: node.js loopbackjs strongloop


【解决方案1】:

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

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

【讨论】:

  • 是的,这行得通!...我以为我已经尝试删除 responsePath。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-18
相关资源
最近更新 更多