admin管理员组

文章数量:1335847

I'm new to python and it seems that all my JSON data is bined with u' prefix as such:

{u'number': u'12345666', u"items"...}

I don't need this data (unicode or whatever) as I want to print the string into a Javascript variable:

var obj = data; // data is the object above.

My python looks something like this;

index.html:
var obj = ${data};

I'm using the moko framework for templating.

// getitems() return {'number':'12312...}
context = {'data': getitems(self)}
self.render_response('index.html',**context)

The processed javascript output data look like this:

var obj = {u'number': u'12345666', u"items"...} 

This is my problem.

I'm new to python and it seems that all my JSON data is bined with u' prefix as such:

{u'number': u'12345666', u"items"...}

I don't need this data (unicode or whatever) as I want to print the string into a Javascript variable:

var obj = data; // data is the object above.

My python looks something like this;

index.html:
var obj = ${data};

I'm using the moko framework for templating.

// getitems() return {'number':'12312...}
context = {'data': getitems(self)}
self.render_response('index.html',**context)

The processed javascript output data look like this:

var obj = {u'number': u'12345666', u"items"...} 

This is my problem.

Share Improve this question edited Mar 28, 2013 at 17:13 Robᵩ 169k20 gold badges249 silver badges323 bronze badges asked Mar 28, 2013 at 16:37 KivyliusKivylius 6,56712 gold badges47 silver badges74 bronze badges 9
  • 6 You are confusing JSON with printing a dictionary. – freakish Commented Mar 28, 2013 at 16:39
  • 3 You do not want to remove the u'', it is only an indication that you are looking at unicode strings. Your templating code will take care of JSON, provided you encode to JSON first. – Martijn Pieters Commented Mar 28, 2013 at 16:39
  • 2 +1: Simply because I don't know why this question has so many downvotes? – freakish Commented Mar 28, 2013 at 16:45
  • @freakish I don't understand it to. They expect everyone to be an expect and people new are simple down voted. – Kivylius Commented Mar 28, 2013 at 17:02
  • 1 @CezarisLT - I don't care about professionalism, I care about munication. In this instance, I had no idea what you were talking about. (Ditto for "tempting" and "procced". I had to guess at your meanings there.) – Robᵩ Commented Mar 28, 2013 at 17:12
 |  Show 4 more ments

1 Answer 1

Reset to default 8

The problem is that you are converting a dictionary to a string (probably Mako does str(...) for you). But you should jsonify it, i.e.

import json
context = { 'data': json.dumps(getitems(self)) }

本文标签: javascriptRemove u39 infront of my JSON dataStack Overflow