Py学习  »  Python

使用python解析json行

rakim • 4 年前 • 551 次点击  

我有这种方法:

lines = rec.split("\n")
rec = ''
size = len(lines)
i=0
for line in lines:
    try:
        self.on_data(json.load(line))
    except:
        logging.warning ('warning, could not parse line:', line)
        if i == size - 1:
            # if it is the last element, we can keep it, since it might not be complete
            rec+=line
    finally:
        i += 1

我得到这个错误:

Message: 'warning, could not parse line:'
Arguments: ('{"readersCount":0,"uuid":"17f5fe87-5140-4f34-ac32-d325beb6b2a1","key":"bar","lockRequestCount":0,"type":"lock","acquired":true}',)

看起来我需要读一个元组的第一个元素还是什么?JSON看起来还好吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38888
 
551 次点击  
文章 [ 1 ]  |  最新文章 4 年前
DirtyBit
Reply   •   1 楼
DirtyBit    5 年前

如评论中所述,您需要 self.on_data(json.loads(line)) :

lines = rec.split("\n")
rec = ''
size = len(lines)
i=0
for line in lines:
    try:
        self.on_data(json.loads(line))
    except:
        logging.warning ('warning, could not parse line:', line)
        if i == size - 1:
            # if it is the last element, we can keep it, since it might not be complete
            rec+=line
    finally:
        i += 1