美文网首页
python obj2json,json2obj

python obj2json,json2obj

作者: hehehehe | 来源:发表于2023-11-19 18:10 被阅读0次
    In [56]: a=AA()
    
    In [57]: a.__dict__
    Out[57]: {}
    
    In [58]: setattr(a,'c','3')
    
    In [59]: a.__dict__
    Out[59]: {'c': '3'}
    
    
    class AA(object):
        def __int__(self, **kwargs):
            self.id = ""
            self.name = ""
            self.__dict__.update(kwargs)
    
        def update(self, **kwargs):
            self.__dict__.update(kwargs)
    
    
    if __name__ == '__main__':
        # a = AA(**{"id": "2", "name": "dds"})
        a = AA()
        a.update(**{"id": "2", "name": "dds"})
        print(a.id)
    
    
    def get_objs_file(file, clz):
        lane_group_json = file2json(file)
        objs = []
        for feature in lane_group_json['features']:
            proper = feature['properties']
            obj = clz()
            for k, v in proper.items():
                setattr(obj, k, v)
            objs.append(obj)
        return objs
    lane_groups = get_objs_file(f"{base_dir}/lane_group.geojson", LaneGroup)
    
    
    def obj2geojson(objs, file, wkt_field):
        features = []
        for d in objs:
            # gpd_data = {'id': d.id, 'geometry': d.drive_line}
            # gpd_data['geometry'] = from_wkt(gpd_data['drive_line'])
            # gpd_datas.append(gpd_data)
            obj_dict = d.__dict__
            geom = None
            if wkt_field:
                geom = from_wkt(obj_dict[wkt_field])
            feature = Feature(geometry=geom, properties=obj_dict )
            features.append(feature)
        fc = FeatureCollection(features)
        json2file(file, dumps(fc))
    
    

    相关文章

      网友评论

          本文标题:python obj2json,json2obj

          本文链接:https://www.haomeiwen.com/subject/sismwdtx.html