【问题标题】:Python JSON decodingPython JSON解码
【发布时间】:2011-03-19 21:49:37
【问题描述】:

我在 python 中解码这个 json 时遇到了一些问题。

从 basehttpserver 我回来了

[
 {
    "changed_aspect": "media", 
    "object": "geography", 
    "object_id": "1306", 
    "subscription_id": 1326, 
    "time": 1300570688
 }
]

我将其放入 simplejsondecoder 中

data = simplejson.loads(s)

但是当我查看数据的长度时,它会返回 1,而不是我期望的 json 对象的 5。

这是所有代码,以防问题出在其他地方。

class httpserver(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_POST(self):
        self.data_string = self.rfile.read(int(self.headers['Content-Length']))
        self.send_response(200)
        self.end_headers()

        data = simplejson.loads(self.data_string)
        print len(data)
        return

【问题讨论】:

    标签: python json simplejson


    【解决方案1】:

    当您解码 JSON 时,您会得到它的样子,即包含单个项目的列表。

    data[0] 应该是您希望看到的字典。

    【讨论】:

    • 好吧,我觉得自己很笨。谢谢安德鲁!
    猜你喜欢
    • 2011-07-21
    • 2016-11-09
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多