def asDict( self ):
if PY_3:<EOL><INDENT>item_fn = self.items<EOL><DEDENT>else:<EOL><INDENT>item_fn = self.iteritems<EOL><DEDENT>def toItem(obj):<EOL><INDENT>if isinstance(obj, ParseResults):<EOL><INDENT>if obj.haskeys():<EOL><INDENT>return obj.asDict()<EOL><DEDENT>else:<EOL><INDENT>return [toItem(v) for v in obj]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return obj<EOL><DEDENT><DEDENT>return dict((k,toItem(v)) for k,v in item_fn())<EOL>
Returns the named parse results as a nested dictionary.

Example::

    integer = Word(nums)
    date_str = integer("year") + '/' + integer("month") + '/' + integer("day")

    result = date_str.parseString('12/31/1999')
    print(type(result), repr(result)) # -> <class 'pyparsing.ParseResults'> (['12', '/', '31', '/', '1999'], {'day': [('1999', 4)], 'year': [('12', 0)], 'month': [('31', 2)]})

    result_dict = result.asDict()
    print(type(result_dict), repr(result_dict)) # -> <class 'dict'> {'day': '1999', 'year': '12', 'month': '31'}

    # even though a ParseResults supports dict-like access, sometime you just need to have a dict
    import json
    print(json.dumps(result)) # -> Exception: TypeError: ... is not JSON serializable
    print(json.dumps(result.asDict())) # -> {"month": "31", "day": "1999", "year": "12"}