Mezzan Mezzan - 5 months ago 30
JSON Question

Tornado JSON output new line each data instead of each character

I cant get to output the json data accordingly. Instead it outputs each character new line. Code:

import tornado.web
import tornado.ioloop
import tornado.httpserver
from tornado.escape import json_encode

class Handler(tornado.web.RequestHandler):
def get(self):
#Sample Json Data
jsondata = '{ "name": "DU-001", "lat": "4.901787", "lng": "114.925919"}' \
',{ "name": "DU-002", "lat": "4.901789", "lng": "114.925929"}'

#Rendering to web file
self.render("web/index.html", title="Lock-On", mydata=json_encode(jsondata))


From HTML code: Loop through mydata to output list

{% for item in mydata %}
{{ item }} <br />
{% end %}


Output result example:

"
{

\
"
n
a
m
e
\
"
:

\
"
D
U
-
0
0
1
\
"

Answer

Make your data a list of dictionaries instead of a string.

jsondata = [{"name": "DU-001", "lat": "4.901787", "lng": "114.925919"},{ "name": "DU-002", "lat": "4.901789", "lng": "114.925929"}]