Re: Web2py using json

2,556 views
Skip to first unread message

Massimo Di Pierro

unread,
Jul 8, 2012, 9:23:04 AM7/8/12
to web...@googlegroups.com
def index():
    from simplejson import loads, dumps
    data =  loads(request.vars.myvar)
    return dumps(data)


On Saturday, 7 July 2012 00:47:32 UTC-5, Akash Kakkar wrote:
Hi,

I want to send string content (html) from client to server using JSON and then decode the JSON to string in python controller, Can somebody help me with the syntax.

Thanks
Akash

vinic...@gmail.com

unread,
Jul 8, 2012, 1:01:44 PM7/8/12
to web...@googlegroups.com
Let's break the scenario in 2 parts.

1) How will you send it to web2py?

a) As a query string via HTTP GET encapsulated in only one param
b) As several params, using a standard query string
c) Via HTTP POST
d) Other (tell us):


2) The web2py part is easy because it's Python, anyway. Just use json
Python module: http://docs.python.org/library/json.html

--
Vinicius Assef

Akash Kakkar

unread,
Jul 13, 2012, 4:34:41 AM7/13/12
to web...@googlegroups.com
Thanks Vinicius, 

Wish I had read your hint earlier, a GET did the job for me, for a newbie GET and POST can be confusing. Infact a little description on GET / POST and when to use would have helped.

howesc

unread,
Jul 13, 2012, 5:57:03 PM7/13/12
to web...@googlegroups.com
for GET/POST/PUT/DELETE/HEAD etc description perhaps start with http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods :)

pumplerod

unread,
Nov 27, 2012, 6:19:35 PM11/27/12
to web...@googlegroups.com
Could you elaborate on how you accomplished this?  I'm also trying to import a json object into my app and parse it.  I'm not sure what that process entails.



On Friday, July 13, 2012 1:34:41 AM UTC-7, Akash Kakkar wrote:

Vinicius Assef

unread,
Nov 28, 2012, 6:50:50 AM11/28/12
to web...@googlegroups.com
Pumplerod, json module is about pure Python.

So, try this in your Python shell, to you see how json module works:

>>> import json
>>> s = json.dumps(dict(nome='john', age=25)) # this line converts a dictionary to a string
>>> print s, type(s)
{"age": 25, "nome": "john"}, <type 'str'>
>>> d = json.loads(s)
>>> print d, type(d)
{u'age': 25, u'nome': u'john'} <type 'dict'>
>>>

Basically, it is.

Probably, you're trying to import a string and you need to convert it
to json, right? This tiny example may help you.

--
Vinicius Assef
> --
>
>
>
Reply all
Reply to author
Forward
0 new messages