【问题标题】:Elasticsearch result from Python client not parsed as json object by javascriptPython客户端的Elasticsearch结果未被javascript解析为json对象
【发布时间】:2016-05-11 08:01:54
【问题描述】:

我无法根据我从 Python (2.7) 客户端获得的 Elasticsearch(2.1.1) 响应创建 Javascript 对象。

Python 代码:

es=Elasticsearch();
@app.route('/output')
def findSpots():
    lat = request.args.get('lati')
    lon = request.args.get('longi')
    res=es.search(index="park_reg", doc_type="parksgn",body={"from": 0, "size": 1, "query": {"filtered": {"filter":  {"geo_distance":  {"distance": "500km","location": {"lat":lat,"lon":lon}}}}}})

return render_template("output.html",lat=lat,lon=lon, res=json.dumps(res))

Javascript 代码:

var res = "{{res}}";
console.log(typeof(res)); //Gives string
document.write(res);
document.write("</br>")
document.write("</br>")

//Tried this as JSON.parse(res) was giving an error
var resStr =JSON.stringify(res);
document.write(resStr);
document.write("</br>")
document.write("</br>")

var resJSON = JSON.parse(resStr);
document.write(resJSON);

回复:

{"hits": {"hits": [{"_score": 1.0, "_type": "parksgn", "_id": "13452823", "_source": {"canParkDays": [""], "x": 965522.6237, "seqnum": 5, "ordernum": "P-087419", "signdesc": "2 HOUR PARKING 9AM-7PM EXCEPT SUNDAY", "canParkDuration": -1, "noParkDays": [""], "SG_MUTCD_C": "R7-182R", "signfc": "", "canParkTime": [""], "location": {"lat": "40.591472", "lon": "-74.06743"}, "arrow": "", "canPark": "true", "y": 154772.6633, "distfromcurb": 310, "borough": "S", "noParkTime": [""], "id": 13452823}, "_index": "park_reg"}], "total": 376893, "max_score": 1.0}, "_shards": {"successful": 5, "failed": 0, "total": 5}, "took": 35, "timed_out": false}

"{"hits": {"hits": [{"_score": 1.0, "_type": "parksgn", "_id": "13452823", "_source": {"canParkDays": [""], "x": 965522.6237, "seqnum": 5, "ordernum": "P-087419", "signdesc": "2 HOUR PARKING 9AM-7PM EXCEPT SUNDAY", "canParkDuration": -1, "noParkDays": [""], "SG_MUTCD_C": "R7-182R", "signfc": "", "canParkTime": [""], "location": {"lat": "40.591472", "lon": "-74.06743"}, "arrow": "", "canPark": "true", "y": 154772.6633, "distfromcurb": 310, "borough": "S", "noParkTime": [""], "id": 13452823}, "_index": "park_reg"}], "total": 376893, "max_score": 1.0}, "_shards": {"successful": 5, "failed": 0, "total": 5}, "took": 35, "timed_out": false}"

{"hits": {"hits": [{"_score": 1.0, "_type": "parksgn", "_id": "13452823", "_source": {"canParkDays": [""], "x": 965522.6237, "seqnum": 5, "ordernum": "P-087419", "signdesc": "2 HOUR PARKING 9AM-7PM EXCEPT SUNDAY", "canParkDuration": -1, "noParkDays": [""], "SG_MUTCD_C": "R7-182R", "signfc": "", "canParkTime": [""], "location": {"lat": "40.591472", "lon": "-74.06743"}, "arrow": "", "canPark": "true", "y": 154772.6633, "distfromcurb": 310, "borough": "S", "noParkTime": [""], "id": 13452823}, "_index": "park_reg"}], "total": 376893, "max_score": 1.0}, "_shards": {"successful": 5, "failed": 0, "total": 5}, "took": 35, "timed_out": false}

我验证了“res”是一个有效的 JSON,但它仍然无法正常工作。 "resJSON" 也给出字符串。

【问题讨论】:

  • 如果res 是响应,请尝试将其单独切片为hits 键的值。您可能需要将此处的 json.dumps(res) 替换为 eval(res.replace("true","True").replace("false","False"))["hits"]["hits"]res,这将是一个字典。
  • javascript sn-p var res = "{{res}}"; 没有意义,您明确将字符串分配给 res 无效 JSON。
  • @keety : 我应该如何访问 res 的值?
  • 你如何从javascript代码调用python客户端,我假设使用getjson行上的东西?您需要发布调用 python api 的代码 sn-p 它会更容易帮助
  • 我正在使用烧瓶。我相信它会在内部处理它

标签: javascript python json parsing elasticsearch


【解决方案1】:

这就是我在控制台上的响应:

"{&#34;hits&#34;: {&#34;hits&#34;: [{&#34;_score&#34;: 1.0, &#34;_type&#34;: &#34;parksgn&#34;, &#34;_id&#34;: &#34;13452823&#34;, &#34;_source&#34;: {&#34;canParkDays&#34;: [&#34;&#34;], &#34;x&#34;: 965522.6237, &#34;seqnum&#34;: 5, &#34;ordernum&#34;: &#34;P-087419&#34;, &#34;signdesc&#34;: &#34;2 HOUR PARKING 9AM-7PM EXCEPT SUNDAY&#34;, &#34;canParkDuration&#34;: -1, &#34;noParkDays&#34;: [&#34;&#34;], &#34;SG_MUTCD_C&#34;: &#34;R7-182R&#34;, &#34;signfc&#34;: &#34;&#34;, &#34;canParkTime&#34;: [&#34;&#34;], &#34;location&#34;: {&#34;lat&#34;: &#34;40.591472&#34;, &#34;lon&#34;: &#34;-74.06743&#34;}, &#34;arrow&#34;: &#34;&#34;, &#34;canPark&#34;: &#34;true&#34;, &#34;y&#34;: 154772.6633, &#34;distfromcurb&#34;: 310, &#34;borough&#34;: &#34;S&#34;, &#34;noParkTime&#34;: [&#34;&#34;], &#34;id&#34;: 13452823}, &#34;_index&#34;: &#34;park_reg&#34;}], &#34;total&#34;: 376893, &#34;max_score&#34;: 1.0}, &#34;_shards&#34;: {&#34;successful&#34;: 5, &#34;failed&#34;: 0, &#34;total&#34;: 5}, &#34;took&#34;: 18, &#34;timed_out&#34;: false}"

只需用双引号 (") 替换 " 就可以了

var res = "{{res}}".split("&#34;").join("\"")
var resJSON = JSON.parse(res)
console.log(typeof(resJSON))

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 2018-08-03
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多