TypeError: string indices must be integers while parsing JSON using Python?

后端 未结 2 1745
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 02:05

I am confuse now why I am not able to parse this JSON string. Similar code works fine on other JSON string but not on this one - I am trying to parse JSON String and extract

相关标签:
2条回答
  • 2020-12-14 02:25

    The problem is that jsonStr is a string that encodes some object in JSON, not the actual object.

    You obviously knew it was a string, because you called it jsonStr. And it's proven by the fact that this line works:

    jsonStr = data.decode("utf-8")
    

    So, jsonStr is a string. Calling json.dumps on a string is perfectly legal. It doesn't matter whether that string was the JSON encoding of some object, or your last name; you can encode that string in JSON. And then you can decode that string, getting back the original string.

    So, this:

    j = json.loads(json.dumps(jsonStr))
    

    … is going to give you back the exact same string as jsonStr in j. Which you still haven't decoded to the original object.

    To do that, just don't do the extra encode:

    j = json.loads(jsonStr)
    

    If that isn't clear, try playing with it an interactive terminal:

    >>> obj = ['abc', {'a': 1, 'b': 2}]
    >>> type(obj)
    list
    >>> obj[1]['b']
    2
    >>> j = json.dumps(obj)
    >>> type(j)
    str
    >>> j[1]['b']
    TypeError: string indices must be integers
    >>> jj = json.dumps(j)
    >>> type(jj)
    str
    >>> j
    '["abc", {"a": 1, "b": 2}]'
    >>> jj
    '"[\\"abc\\", {\\"a\\": 1, \\"b\\": 2}]"'
    >>> json.loads(j)
    ['abc', {'a': 1, 'b': 2}]
    >>> json.loads(j) == obj
    True
    >>> json.loads(jj)
    '["abc", {"a": 1, "b": 2}]'
    >>> json.loads(jj) == j
    True
    >>> json.loads(jj) == obj
    False
    
    0 讨论(0)
  • 2020-12-14 02:31

    Try replacing j = json.loads(json.dumps(jsonStr)) with j = json.loads(jsonStr).

    0 讨论(0)
提交回复
热议问题