[英]Extract all keys from JSON string which is a key value pair
I have a pretty simple JSON string as shown below我有一个非常简单的 JSON 字符串,如下所示
x = '{ "Text1":"Value1", "Text2":"Value2", "Text3":"Value3"}'
x = '{ "Text1":"Value1", "Text2":"Value2", "Text3":"Value3"}'
I would like to store all keys in one list and all values in another list.我想将所有键存储在一个列表中,并将所有值存储在另一个列表中。 I do not want to use a loop as this json is going to be very huge with KVP
我不想使用循环,因为使用 KVP 时这个 json 会非常庞大
I tried to get help from google but haven't got anything which satisfies the requirement.我试图从谷歌获得帮助,但没有任何满足要求的东西。
If it is not a nested JSON then如果它不是嵌套的 JSON 那么
d = dict({ "Text1":"Value1", "Text2":"Value2", "Text3":"Value3"})
print(d.keys());
You can try this:你可以试试这个:
import json
x = '{ "Text1":"Value1", "Text2":"Value2", "Text3":"Value3"}'
keys, values = map(list, zip(*json.loads(x).items()))
print(keys) # ['Text1', 'Text2', 'Text3']
print(values) # ['Value1', 'Value2', 'Value3']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.