json character string
menu = \
{
"breakfast":
{
"hours":
"7-11",
"items":
{
"breakfast burritos":
"$6.00",
"pancakes":
"$4.00"
}
},
"lunch"
:
{
"hours":
"11-3",
"items":
{
"hamburger":
"$5.00"
}
},
"dinner":
{
"hours":
"3-10",
"items":
{
"spaghetti":
"$8.00"
}
}
}
import json
menu_json = json.dumps(menu)
menu_json
menu2 = json.loads(menu_json)
# It can be interpreted as python structure
import datetime
now = datetime.datetime.utcnow()
json.dumps(now)
# Unable to convert , Because of the standard json No date defined
# transformation
now_str = str(now)
json.dumps(now_str)
# You can convert
from time import mktime
now_epoch = int(mktime(now.timetuple()))
json.dumps(now_epoch)
# Can convert epoch value
class
DTEncoder(json.JSONEncoder):
# Inherit overloads default Method
def default(self, obj):
# isinstance() Check obj The type of
if isinstance(obj, datetime.datetime):
return int(mktime(obj.timetuple()))
# Otherwise, it's what ordinary decoders know :
return json.JSONEncoder.default(self, obj)
json.dumps(now, cls=DTEncoder)