简体   繁体   中英

Extracting Json Objects from string

Lets say I have an incoming string oversocket and it look like this

'text.....text {"foo": {"bar":100}} text {"bar":2} test {"foo"'

What is the best way/library to extract only the json objects out of the incoming string?

I have tried simplejson.JSONDecoder from simple json library. However, it is not only finding objects or I didn't know how to use it.

I have tried something like this so far

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:]

Pure python solution based on simple bracket counting and try ing to parse gathered text.

import json

inp = list(r'text {"foo": {"bar":100}} text {"bar":2} test ')
stack = ''
bracket_counter = 0
jsons = []

while inp:
    char = inp.pop(0)
    if char == '{':
        bracket_counter += 1

    if bracket_counter:
        stack += char

    if char == '}':
        bracket_counter -= 1
        if bracket_counter == 0:
            try:
                parsed = json.loads(stack)
                jsons.append(parsed)
            except json.JSONDecodeError:
                pass
            stack = ''

print(jsons)  # -> [{'foo': {'bar': 100}}, {'bar': 2}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM