Gae, Python apps, SNS, Web Raiders

Source: Internet
Author: User
Keywords Then string encode then string encode
Tags .net ajax app apps content data data structures display

Today, we realized that Chinese data were extracted from the datastore database of Google App engine and then sent the requested return value in JSON format as Xnjs Ajax object back to the net, and the normal display in the Web page (Khan ...). It really took a while to organize the sentence composition.

The data that the XNJS Ajax object accepts in JSON mode is a plain string of JSON-formatted content. Although the Web page of the campus is UTF-8 encoded, the default storage encoding for Google APP engine datastore is Unicode.
At first I thought that the Chinese data from Datastore would need to be processed with Data.encode (' UTF8 ') and then converted to string as the result of the AJAX request, but the returned data would be garbled on the campus page. This makes me baffled: if I don't use encode (' UTF8 '), str () will prefix each string with a "u" character when converting to Unicode, which will destroy the JSON format! Do you want to remove these pesky u by hand? This is too incompatible with the philosophy of Python! (Too much trouble!) )

In desperation I had to go around asking for help, and realized that the people who were really using XNJS this Ajax object were really few. Backstage shelves in Google App engine is even more scarce (I doubt who else, except me). But at last I learned the key to the problem (thank Houyongr): I used the wrong way to convert Python's built-in data structures dict to JSON format strings. The original STR () is not solve the problem, advance conversion UTF8 is also wrong-the right way should be with the child's powerful Simplejson module!

Google APP engine Environment Simplejson is not in the Python standard module path, but in Django.utils. The purpose of implementing this article is actually simple: to query the desired string from the datastore, and then use it to create any required Python data structures (of course, for JSON's needs, the outermost layer must be dict), and then through Simplejson.dumps () Convert it to a target string and return it to the school. The middle of all the coding process to python/django/gae/xnjs and so on to deal with, we do not have to worry about the conversion between Unicode and UTF8.

Actually speaking of which I still don't understand, when did Unicode become UTF8?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.