簡體   English   中英

Python客戶端未將Python客戶端的Elasticsearch結果解析為json對象

[英]Elasticsearch result from Python client not parsed as json object by javascript

我無法從從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”也給出了字符串。

因此,這就是我在控制台上的響應:

"{&#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}"

只需用雙引號(“)替換&#34就可以了

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM