【发布时间】:2020-06-18 00:47:46
【问题描述】:
假设我有一个传入的字符串 oversocket,它看起来像这样
'text.....text {"foo": {"bar":100}} text {"bar":2} test {"foo"'
从传入字符串中仅提取 json 对象的最佳方法/库是什么?
我已经尝试过 simplejson 库中的 simplejson.JSONDecoder。但是,它不仅仅是寻找对象,或者我不知道如何使用它。
到目前为止,我已经尝试过这样的事情
import simplejson as json
input_buffer = ""
def in_data(data):
input_buffer += data
try:
dict, idx = json.JSONDecoder().raw_decode(input_buffer)
except:
#handle exception in case nothing found
self.handle_input(dict) #send the dictionary for processing
input_buffer = input_buffer[idx:]
【问题讨论】:
-
到目前为止您尝试过什么?显示一些代码。
标签: python json python-3.x simplejson