Skip to main content
 首页 » 编程设计

python之从 Json 字符串创建 Python 对象

2025年05月04日24sky-heaven

<分区>

到 SeismicPortal 的 websocket 连接正在向我发送打包在 JSON 对象中的地震数据,我将其作为多行字符串获取,例如:

{                                                                                                                                                                                                
    "action": "create",                                                                                                                                                                          
    "data": {                                                                                                                                                                                    
        "geometry": {                                                                                                                                                                            
            "coordinates": [                                                                                                                                                                     
                -95.12,                                                                                                                                                                          
                16.52,                                                                                                                                                                           
                -52.0                                                                                                                                                                            
            ],                                                                                                                                                                                   
            "type": "Point"                                                                                                                                                                      
        },                                                                                                                                                                                       
        "id": "20180303_0000046",                                                                                                                                                                
        "properties": {                                                                                                                                                                          
            "auth": "UNM",                                                                                                                                                                       
            "depth": 52.0,                                                                                                                                                                       
            "evtype": "ke",                                                                                                                                                                      
            "flynn_region": "OAXACA, MEXICO",                                                                                                                                                    
            "lastupdate": "2018-03-03T10:26:00.0Z",                                                                                                                                              
            "lat": 16.52,                                                                                                                                                                        
            "lon": -95.12,                                                                                                                                                                       
            "mag": 4.0,                                                                                                                                                                          
            "magtype": "m",                                                                                                                                                                      
            "source_catalog": "EMSC-RTS",                                                                                                                                                        
            "source_id": "652127",                                                                                                                                                               
            "time": "2018-03-03T07:09:05.0Z",                                                                                                                                                    
            "unid": "20180303_0000046"                                                                                                                                                           
        },                                                                                                                                                                                       
        "type": "Feature"                                                                                                                                                                        
    }                                                                                                                                                                                            
} 

我想将字符串中的数据转换为 python 对象。

正如您在 JSON 数据中看到的那样,有很多嵌套。当我定义类及其嵌入性以构建一个结构的对象时,该结构将保存来自 JSON 的所有数据,我在想也许有一些神奇的 Python 函数 jsonStringToObject 可以定制一个类和所有子类都需要将所有数据保存在 JSON 中并创建它的实例。

让我们在变量 rawData 中获取原始 JSON 字符串:

rawData = """{"action":"create","data":{"geometry": {"type": "Point","coordinates": [... """ 

现在我必须这样做:

>>> import json 
>>> quake = json.loads(rawData) 
>>> quake['data']['properties']['flynn_region'] 
"OXACA_MEXICO" 

但语法中充满了方括号和撇号。

我希望我可以像这样访问数据:

>>> import json     
>>> quake = jsonStringToObject(rawData) 
>>> quake.data.properties.flynn_region 
"OXACA_MEXICO"