Jsonifying bits from MySQLdb in Python -
i'm reading mysql record using mysqldb
in python. 1 of columns bit. it's shows either \\u0000
or \\u0001
on python console when utilize json.dumps
covert json (it's shows \x00
before conversion) , send browser, browser shows nothing. i'm assuming it's because doesn't know character unicode.
is there way me javascript true
or false
these values manipulation @ either client or server ?
manipulating server side simple as:
import json def convert_to_bool(bstring): homecoming true if bstring == b'\x00' else false
example usage:
your_byte_string = b'\x00' # can write # your_byte_string = u'\u0000' print(json.dumps(convert_to_bool(your_byte_string))) # >>> true your_byte_string = b'\x01' # can write # your_byte_string = u'\u0001' print(json.dumps(convert_to_bool(your_byte_string))) # >>> false
then, may want add together code -say- show image if field true. can done both @ server side or @ client side.
server side
if not plan utilize boolean variable in javascript code, can send straight expected html:
if convert_to_bool(your_byte_string): # here i'm printing should utilize dumped string json response print(json.dumps('the field <b>true</b>!')) else: print(json.dumps('unfortunately field <b>false</b>.'))
client side
if using json suppose know how that, it's basic javascript, sake of completeness here code:
// javascript code // yourdata field value, retrieved ajax phone call if (yourdata) { alert('the field true!'); } else { alert('the field false!'); }
python mysql-python
No comments:
Post a Comment