将JSON数组转换为Python列表
问题描述:
import json
array = '{"fruits": ["apple", "banana", "orange"]}'
data = json.loads(array)
那是我的JSON数组,但是我想将Fruits字符串中的所有值转换为Python列表.正确的方法是什么?
That is my JSON array, but I would want to convert all the values in the fruits string to a Python list. What would be the correct way of doing this?
答
import json
array = '{"fruits": ["apple", "banana", "orange"]}'
data = json.loads(array)
print data['fruits']
# the print displays:
# [u'apple', u'banana', u'orange']
您拥有了所需的一切. data
将是字典,data['fruits']
将是列表
You had everything you needed. data
will be a dict, and data['fruits']
will be a list