python json 编码

# -*- 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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值