您的位置:首页 > 编程语言 > Python开发

python json 编码

2016-12-08 16:55 337 查看
# -*- coding=utf-8 -*-

import os
import json

class info:
def __init__(self):
self.year = 2014
self.version = '1.0'
self.description = 'This is stable 1.0 version of the 2014 MS COCO dataset.'
self.contributor = 'Microsoft COCO group'
self.url = 'http://mscoco.org'
self.date_created = '2015-01-27 09:11:52.357475'

def __repr__(self):
return repr((self.description, self.url, self.version, self.year, self.contributor, self.date_created))

class image:
def __init__(self, id, width, height, file_name, license, flickr_url, coco_url, date_captured):
self.id = id
self.width = width
self.height = height
self.file_name = file_name
self.license = license
self.flickr_url = flickr_url
self.coco_url = coco_url
self.date_captured = date_captured

def __repr__(self):
return repr((self.license, self.file_name, self.coco_url, self.height, self.width, self.date_captured,
self.flickr_url, self.id))

class license:
def __init__(self, id, name, url):
self.id = id
self.name = name
self.url = url

def __repr__(self):
return repr((self.id, self.name, self.url))

class annotation:
def __init__(self, id, image_id, caption):
self.id = id
self.image_id = image_id
self.caption = caption

def __repr__(self):
return repr((self.id, self.image_id, self.caption))

def dump_json():
info_ = info()
images = [image(57870, 640, 480, 'train2014_70.jpg', 5,
'http://farm4.com/z.jpg', 'http://org/57870', '2013-11.14'),
image(57871, 641, 481, 'train2014_71.jpg', 6,
'http://farm4.com/a.jpg', 'http://org/57871', '2013-11-15'),
]

annotations = [annotation(48, 57870, 'A very clean and well decorated empty bathroom'),
annotation(49, 57871, 'A panoramic view of a kitchen and all of its applications'), ]

licenses = [license(5, 'A', 'http://mscoco.org'),
license(6, 'B', 'http://mscoco.org'), ]

# bigD = {"info": info.__dict__, "images": images.__dict__, "annotations": annotations.__dict__, "licenses": licenses.__dict__}
bigD = {"info": info_, "images": images, "annotations": annotations, "licenses": licenses}
json_str = json.dumps(bigD, default=lambda o: o.__dict__, sort_keys=True, indent=4)
# json_str = json.dumps(bigD)
print(json_str)

def put_data_into_json():

def main():
# dump_json()

if __name__ == '__main__':
main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: