我应该如何从json字符串的数组对象下面解析“text”值?

时间:2018-03-13 09:51:32

标签: python json

[“{\”contributors \“:null,\”truncated \“:false,\”text \“:\”RT @itisprashanth:所有关于#jallikattu的请愿明天将由最高法院审理。明天将是所有青年的D日\ u226 \“}”]

2 个答案:

答案 0 :(得分:2)

使用 json 模块将字符串转换为json对象。

<强>实施例

import json
h = ["{\"contributors\": null, \"truncated\": false, \"text\": \"RT @itisprashanth: All petitions regarding #jallikattu to be heard by Supreme Court tomorrow. Tomorrow will be the D-Day for all the Youth\u2026\"}"]
for i in h:
    v = json.loads(i)
    print v["text"]

<强>输出:

RT @itisprashanth: All petitions regarding #jallikattu to be heard by Supreme Court tomorrow. Tomorrow will be the D-Day for all the Youth…

答案 1 :(得分:1)

将字符串(给定数组的第一个元素)解析为JSON对象,并获取其文本值。

import json
arr = ["{"contributors": null, "truncated": false, "text": "RT @itisprashanth: All petitions regarding #jallikattu to be heard by Supreme Court tomorrow. Tomorrow will be the D-Day for all the Youth\u2026"}"]
print(json.loads(arr[0])["text"])
# "RT @itisprashanth: All petitions regarding #jallikattu to be heard by Supreme Court tomorrow. Tomorrow will be the D-Day for all the Youth\u2026"