【发布时间】: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