在Python中解码StdClass

时间:2013-03-13 19:54:28

标签: python wordpress object dictionary stdclass

我使用Python使用urllib查询WordPress.org API。反过来,WordPress.org决定使用编码的PHP StdClass对象进行响应。如果我使用PHP会很棒,但我必须使用Python。知道如何把它变成字典(没有 s:10:“slijekgdds”前缀)吗?

apiUrl = "http://api.wordpress.org/themes/info/1.0/"
body = urllib.urlencode({
    'action': "query_themes",
    'request': {
        "browse": "featured", 
        'fields': {
            'download_link': True,
            'preview_url': False,
            'screenshot_url': False,
            'description': False,
            'sections': False,
            'screenshots': False,
            'downloaded': False,
            'screenshot_count': False
        }
    }
}, True)

request = urllib.urlopen(apiUrl, body)
response = request.read(200)

这是返回(截断):

O:8:"stdClass":2:{
    s:4:"info";
    a:3:{
        s:4:"page";
        i:1;
        s:5:"pages";
        i:41;
        s:7:"results";
        i:610;
    }
    s:6:"themes";
    a:15{
        i:0;
        O:8:"stdClass":10:{
            #BLAH BLAH
        }
    }
}

1 个答案:

答案 0 :(得分:0)

基于this,您必须使用pip或某些东西安装,我猜这是你想要的:

from phpserialize import loads, phpobject

python_object = loads(php_serialized_string, object_hook=phpobject)