开发者

Passing a Python list using JSON and Django

I'm trying to send a Python list in to client side (encoded as JSON开发者_StackOverflow). This is the code snippet which I have written:

array_to_js = [vld_id, vld_error, False]
array_to_js[2] = True
jsonValidateReturn = simplejson.dumps(array_to_js)
return HttpResponse(jsonValidateReturn, mimetype='application/json')

How do I access it form client side? Can I access it like the following?

jsonValidateReturn[0]

Or how do I assign a name to the returned JSON array in order to access it?

Actually I'm trying to convert a server side Ajax script that returns an array (see Stack Overflow question Creating a JSON response using Django and Python that handles client side POST requests, so I wanted the same thing in return with Python, but it didn't go well.


The JSON array will be dumped without a name / assignment.

That is, in order to give it a name, in your JavaScript code you would do something like this:

var my_json_data_dump = function_that_gets_json_data();

If you want to visualize it, for example, substitute:

var my_json_data_dump = { 'first_name' : Bob, 'last_name': smith };

Also, like Iganacio said, you're going to need something like json2.js to parse the string into the object in the last example. You could wrap that parsing step inside of function_that_gets_json_data, or if you're using jQuery you can do it with a function like jQuery.getJSON().

json2.js is still nice to have, though.


In response to the comment (I need space and markup):

Yes, of course. All the Python side is doing is encoding a string representation (JSON) for you. You could do something like 'var blah = %s' % json.dumps(obj_to_encode) and then on the client side, instead of simply parsing the response as JSON, you parse it as JavaScript.

I wouldn't recommend this for a few reasons:

  1. You're no longer outputting JSON. What if you want to use it in a context where you don't want the variable name, or can't parse JavaScript?
  2. You're evaluating JavaScript instead of simply parsing JSON. It's an operation that's open to security holes (if someone can seed the data, they might be able to execute a XSS attack).

I guess you're facing something I think every Ajax developer runs in to. You want one place of truth in your application, but now you're being encouraged to define variables and whatnot in JavaScript. So you have to cross reference your Python code with the JavaScript code that uses it.

I wouldn't get too hung up on it. I can't see why you would absolutely need to control the name of the variable from Python in this manner. If you're counting on the variable name being the same so that you can reference it in subsequent JavaScript or Python code, it's something you might obviate by simply restructuring your code. I don't mean that as a criticism, just a really helpful (in general) suggestion!


If both client and server are in Python, here's what you need to know.

Server. Use a dictionary to get labels on the fields. Write this as the response.

>>> import json
>>> json.dumps( {'vld_id':1,'vls_error':2,'something_else':True} )
'{"vld_id": 1, "something_else": true, "vls_error": 2}'

Client. After reading the response string, create a Python dictionary this way.

>>> json.loads( '{"vld_id": 1, "something_else": true, "vls_error": 2}' )
{u'vld_id': 1, u'something_else': True, u'vls_error': 2}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜