Reputation: 63299
I have a python FastCGI server which accepts POST from client, I want to use JSON as the format to communicate between the client and server, but it really confuse me how it works.
The JSON representation in javascript is:
{'login_user': {'username':username, 'password':password}}
When I post this JSON object, the jquery will re-encode it to dict and convert it to string, encode it and send it out:
login_user[username] = "kajjd"&loginuser[password] = "kdjfdj" #it's unescaped query string
My python server will receive this string. I also want to use JSON in the server side, what I can do to convert this query string back to JSON in python? Is it any fast way?
I don't know my usage of JSON is good way, but I can't see any good side of JSON, why not XML? I am asking myself.
Upvotes: 0
Views: 466
Reputation: 22663
XML for communication for a web-app? Take a deep breath and think about it. If you have doubts about it, then it means you really have no need for it. You rely on XML for fairly complex data sets with potential heavy modifications to the format of a protocol to be foreseen over time. By nature XML would make this OK: morphing the data, producing different versions, allowing for self-describing formats to be parsed by code who wouldn't need to detect this itself and know what to do about it. Otherwise prefer something more lightweight (and that could still achieve pretty much the same thing). That's why JSON is a good alternative: it's still more compact, and relatively easy to manipulate both by a human and a machine. If you were to need to reduce bandwidth a lot, then look at binary formats.
Now moving on to the real question: See this answer for JSON usage in JavaScript and Java. They're all bi-directional libraries supporting decoding and encoding.
Now regarding your form: if you want to use JSON, why do you transform the string this dict format?
To get data from the server, either do a normal AJAX request and parse or eval your returned value (parsing preferred, if possible with native APIs), or use something like jQuery.getJSON. Or jquery-json.
To send the request to the server, lookup jQuery.ajax() or jQuery.post().
You may also want to look at these questions:
Upvotes: 2