您有有效的JSON數據已經。 \"
是一個轉義報價(因此它是字符串值的一部分),並且[...]
是JSON 數組(類似於Python列表)。
只需加載數據的JSON:
>>> import json
>>> jsondata = r'''{"quotes":["I could calculate your chance of survival, but you won't like it.","I'd give you advice, but you wouldn't listen. No one ever does.","I ache, therefore I am.","I've seen it. It's rubbish. (About a Magrathean sunset that Arthur finds magnificent)","Not that anyone cares what I say, but the Restaurant is on the other end of the universe.","I think you ought to know I'm feeling very depressed.","My capacity for happiness,\" he added, \"you could fit into a matchbox without taking out the matches first.","Arthur: \"Marvin, any ideas?\" Marvin: \"I have a million ideas. They all point to certain death.\"","\"What's up?\" [asked Ford.] \"I don't know,\" said Marvin, \"I've never been there.\"","Marvin: \"I am at a rough estimate thirty billion times more intelligent than you. Let me give you an example. Think of a number, any number.\" Zem: \"Er, five.\" Marvin: \"Wrong. You see?\"","Zaphod: \"Can it Trillian, I'm trying to die with dignity. Marvin: \"I'm just trying to die.\""]}'''
>>> data = json.loads(jsondata)
>>> data
{'quotes': ["I could calculate your chance of survival, but you won't like it.", "I'd give you advice, but you wouldn't listen. No one ever does.", 'I ache, therefore I am.', "I've seen it. It's rubbish. (About a Magrathean sunset that Arthur finds magnificent)", 'Not that anyone cares what I say, but the Restaurant is on the other end of the universe.', "I think you ought to know I'm feeling very depressed.", 'My capacity for happiness," he added, "you could fit into a matchbox without taking out the matches first.', 'Arthur: "Marvin, any ideas?" Marvin: "I have a million ideas. They all point to certain death."', '"What\'s up?" [asked Ford.] "I don\'t know," said Marvin, "I\'ve never been there."', 'Marvin: "I am at a rough estimate thirty billion times more intelligent than you. Let me give you an example. Think of a number, any number." Zem: "Er, five." Marvin: "Wrong. You see?"', 'Zaphod: "Can it Trillian, I\'m trying to die with dignity. Marvin: "I\'m just trying to die."']}
>>> from pprint import pprint
>>> pprint(data)
{'quotes': ["I could calculate your chance of survival, but you won't like it.",
"I'd give you advice, but you wouldn't listen. No one ever does.",
'I ache, therefore I am.',
"I've seen it. It's rubbish. (About a Magrathean sunset that "
'Arthur finds magnificent)',
'Not that anyone cares what I say, but the Restaurant is on the '
'other end of the universe.',
"I think you ought to know I'm feeling very depressed.",
'My capacity for happiness," he added, "you could fit into a '
'matchbox without taking out the matches first.',
'Arthur: "Marvin, any ideas?" Marvin: "I have a million ideas. '
'They all point to certain death."',
'"What\'s up?" [asked Ford.] "I don\'t know," said Marvin, "I\'ve '
'never been there."',
'Marvin: "I am at a rough estimate thirty billion times more '
'intelligent than you. Let me give you an example. Think of a '
'number, any number." Zem: "Er, five." Marvin: "Wrong. You see?"',
'Zaphod: "Can it Trillian, I\'m trying to die with dignity. '
'Marvin: "I\'m just trying to die."']}
>>> import random
>>> print(random.choice(data['quotes']))
I've seen it. It's rubbish. (About a Magrathean sunset that Arthur finds magnificent)
>>> print(random.choice(data['quotes']))
I ache, therefore I am.
在我使用了random.choice()
function挑選從列表中引號的一個隨機上面演示。
唯一缺少的是馬文的催眠曲,我所有的馬文的utterings的最愛:
現在世界已上牀睡覺
黑暗不會吞噬我的頭
我可以通過紅外線見
我怎麼恨晚
現在我躺下睡覺
嘗試數電氣羊
甜夢希望你能磕ep
我如何討厭夜晚
其實......這是一個有效的json,它給你一個帶引號列表的字典。例如,'data [「quotes」] [0]'是''我可以計算出你的生存期,但你不會喜歡它。「你得到這些'\'轉義字符,因爲json字符串具有嵌入字符串。 – tdelaney
順便說一句,它很高興看到你有來自世界上一些最偉大的文學報價。 – tdelaney