类型错误:使用 Python 解析 JSON 时,字符串索引必须是整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20084779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: string indices must be integers while parsing JSON using Python?
提问by AKIWEB
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 script from the JSON.
我现在很困惑为什么我无法解析这个 JSON 字符串。类似的代码适用于其他 JSON 字符串,但不适用于这个字符串 - 我正在尝试解析 JSON 字符串并从 JSON 中提取脚本。
Below is my code.
下面是我的代码。
for step in steps:
step_path = '/example/v1' +'/'+step
data, stat = zk.get(step_path)
jsonStr = data.decode("utf-8")
print(jsonStr)
j = json.loads(json.dumps(jsonStr))
print(j)
shell_script = j['script']
print(shell_script)
So the first print(jsonStr)will print out something like this -
所以第一个print(jsonStr)会打印出这样的东西 -
{"script":"#!/bin/bash\necho Hello world1\n"}
And the second print(j)will print out something like this -
第二个print(j)会打印出这样的东西 -
{"script":"#!/bin/bash\necho Hello world1\n"}
And then the third print doesn't gets printed out and it gives this error -
然后第三次打印没有被打印出来,它给出了这个错误 -
Traceback (most recent call last):
File "test5.py", line 33, in <module>
shell_script = j['script']
TypeError: string indices must be integers
So I am wondering what wrong I am doing here?
所以我想知道我在这里做错了什么?
I have used same above code to parse the JSON and it works fine..
我使用上面相同的代码来解析 JSON,它工作正常..
采纳答案by 1st1
Try replacing j = json.loads(json.dumps(jsonStr))with j = json.loads(jsonStr).
尝试替换j = json.loads(json.dumps(jsonStr))为j = json.loads(jsonStr).
回答by abarnert
The problem is that jsonStr is a string that encodes some object in JSON, not the actual object.
问题是 jsonStr 是一个字符串,它用 JSON 对某个对象进行编码,而不是实际的对象。
You obviously knewit was a string, because you called it jsonStr. And it's proven by the fact that this line works:
您显然知道它是一个字符串,因为您将其称为jsonStr。这条线有效的事实证明了这一点:
jsonStr = data.decode("utf-8")
So, jsonStris a string. Calling json.dumpson 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.
所以,jsonStr是一个字符串。调用json.dumps字符串是完全合法的。该字符串是某个对象的 JSON 编码还是您的姓氏并不重要;您可以在 JSON 中对该字符串进行编码。然后您可以解码该字符串,取回原始字符串。
So, this:
所以这:
j = json.loads(json.dumps(jsonStr))
… is going to give you back the exact same string as jsonStrin j. Which you still haven't decodedto the original object.
...是要退给你完全一样的字符串jsonStr在j。您还没有解码为原始对象。
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

