0

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 
\ 
"
Mezzan
  • 329
  • 1
  • 5
  • 18

1 Answers1

1

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"}]

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • That works well together with removing json_encode(). Thanks mate! – Mezzan Jun 23 '16 at 13:31
  • You may want the json encoding because without it, you're actually printing python dictionaries, which aren't completely correct JSON – OneCricketeer Jun 23 '16 at 13:33
  • I tried including json_encoding however the output goes back to showing the same issue. anything I am missing here? – Mezzan Jun 23 '16 at 13:41
  • 1
    Oh, right, because that puts it back to a string... Umm, I think you can do `json_encode(item)` within the template. Otherwise, JSON doesn't really care about line breaks, so it's not clear why you need to do that. You can loop over the list and extract the values out of those dictionaries. – OneCricketeer Jun 23 '16 at 13:44
  • That make sense. Putting json_encode(item) works again. This is perfect. Thanks alot mate. – Mezzan Jun 23 '16 at 14:00