将JSON字符串转换为字典未列出 [英] Converting JSON String to Dictionary Not List

查看:343
本文介绍了将JSON字符串转换为字典未列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



到目前为止,我已经完成了以下工作:

  json1_file = open('json1')
json1_str = json1_file.read()
json1_data = json.loads(json1_str)

我期待 json1_data 成为 dict 类型,但实际上它作为一个列表类型,当我检查它与 type( json1_data)



我缺少什么?我需要这是一个字典,所以我可以访问一个键。

解决方案

你的JSON是一个内部有一个对象的数组,所以当你读取它时,你会得到一个字典里面的列表。您可以通过访问列表中的项目0来访问您的字典,如下所示:

  json1_data = json.loads(json1_str)[ 0] 

现在,您可以访问数据点数据中存储的数据,就像您期待:

  datapoints = json1_data ['datapoints'] 







我还有一个问题,如果有人可以咬:我正在尝试平均这些数据点中的第一个元素(即数据点[0] [0]))。只是列出它们,我尝试做数据点[0:5] [0],但我得到的是第一个数据点,两个元素都不同于想要获取只包含第一个元素的前5个数据点。有没有办法做到这一点?


datapoints [0:5] [0] 你在期待 datapoints [0:5] 返回一个仅包含前5个元素的新列表片段,然后添加 [0] 最后,它将仅仅是从结果列表片段中的第一个元素。您需要用来获取所需结果的是列表解析

  [p [0] for p in datapoints [0:5]] 
pre>

这是一个简单的方法来计算平均值:

  sum (p [0] for p in data point [0:5])/ 5。 #结果是35.8 

如果您愿意安装 NumPy ,那就更容易了:

  import numpy 
json1_file =打开('json1')
json1_str = json1_file.read()
json1_data = json.loads(json1_str)[0]
datapoints = numpy.array(json1_data ['datapoints'])
avg = datapoints [0:5,0] .mean()
#avg现在是35.8

使用具有NumPy数组的切片语法的操作符具有您最初期望使用列表切片的行为。


I am trying to pass in a JSON file and convert the data into a dictionary.

So far, this is what I have done:

     json1_file = open('json1')
     json1_str = json1_file.read()
     json1_data = json.loads(json1_str)

I'm expecting json1_data to be a dict type but it actually comes out as a list type when I check it with type(json1_data).

What am I missing? I need this to be a dictionary so I can access one of the keys.

解决方案

Your JSON is an array with a single object inside, so when you read it in you get a list with a dictionary inside. You can access your dictionary by accessing item 0 in the list, as shown below:

json1_data = json.loads(json1_str)[0]

Now you can access the data stored in datapoints just as you were expecting:

datapoints = json1_data['datapoints']


I have one more question if anyone can bite: I am trying to take the average of the first elements in these datapoints(i.e. datapoints[0][0]). Just to list them, I tried doing datapoints[0:5][0] but all I get is the first datapoint with both elements as opposed to wanting to get the first 5 datapoints containing only the first element. Is there a way to do this?

datapoints[0:5][0] doesn't do what you're expecting. datapoints[0:5] returns a new list slice containing just the first 5 elements, and then adding [0] on the end of it will take just the first element from that resulting list slice. What you need to use to get the result you want is a list comprehension:

[p[0] for p in datapoints[0:5]]

Here's a simple way to calculate the mean:

sum(p[0] for p in datapoints[0:5])/5. # Result is 35.8

If you're willing to install NumPy, then it's even easier:

import numpy
json1_file = open('json1')
json1_str = json1_file.read()
json1_data = json.loads(json1_str)[0]
datapoints = numpy.array(json1_data['datapoints'])
avg = datapoints[0:5,0].mean()
# avg is now 35.8

Using the , operator with the slicing syntax for NumPy's arrays has the behavior you were originally expecting with the list slices.

这篇关于将JSON字符串转换为字典未列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆