[英]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”也給出了字符串。
因此,這就是我在控制台上的響應:
"{"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": 18, "timed_out": false}"
只需用雙引號(“)替換&#34就可以了
var res = "{{res}}".split(""").join("\"")
var resJSON = JSON.parse(res)
console.log(typeof(resJSON))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.