顯示具有 json 標籤的文章。 顯示所有文章
顯示具有 json 標籤的文章。 顯示所有文章

2014年12月23日 星期二

PHP echo json_encode error

Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at )


origin :
...
echo json_encode($myarray);

fix :
...
header("Content-type: application/json; charset=utf-8");
echo json_encode($myarray);
die;









References :
Output.php with compress_output = true, always calls ob_start(‘ob_gzhandler’) causing warning with ajax / Forums / Community / EllisLab

2014年9月15日 星期一

Python json.dumps 時含有時間物件

TypeError: datetime.datetime(2014, 9, 16, 10, 24, 26, 755110) is not JSON serializable


class DateEncoder(json.JSONEncoder):
    def default(self, obj):
        if hasattr(obj, 'isoformat'):
            return obj.isoformat()
        else:
            return str(obj)
        return json.JSONEncoder.default(self, obj)


json.dumps(my_variable, cls=DateEncoder)




References :
python - Cannot serialize datetime as JSON from Cherrypy - Stack Overflow

2014年8月31日 星期日

python read json file

import json

f = open('addressbook.json')

for addressbook in json.load(f):
    print addressbook['name']
    print addressbook['phone']




References :
http://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file-in-python