Pyparsing корабли с примером анализа JSON (или вы можете получить его онлайн здесь ):
>>> text = r"""{
... "oneliners": [
... "she\'s the one",
... "who opened the gates"
... ]
... } """
>>> text
'{ \n "oneliners": [ \n "she\\\'s the one", \n "who opened the gates" \n ] \n} '
>>> obj = jsonObject.parseString(text)
>>> obj.asList()
[['oneliners', ["she\\'s the one", 'who opened the gates']]]
>>> obj.asDict()
{'oneliners': (["she\\'s the one", 'who opened the gates'], {})}
>>> obj.oneliners
(["she\\'s the one", 'who opened the gates'], {})
>>> obj.oneliners.asList()
["she\\'s the one", 'who opened the gates']
Не пугайтесь кажущегося включения dict ('{}') в obj.oneliners
, который является просто выводом repr для pyparsing объекта ParseResults. Вы можете просто обращаться с obj.oneliners как с обычным списком или, если хотите, извлекать его содержимое в виде списка, используя asList
, как показано.