本文介绍了将Python中的JSON解析为字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量研究后,有点迷失了。我的代码下面解析JSON到一个字典我以为使用json加载

  response = json.load(MSW)# - >将采用JSON String&把它变成一个python dict 

使用下面的迭代我返回一个这样很好的系列p>

  {u'swell':{u'components':{u'primary':{u'direction':222.5}}} } 
{u'swell':{u'components':{u'primary':{u'direction':221.94}}}

ourResult = response
for rs在我们的结果:
打印rs

但是如何哦,如何访问222.5值。上面看起来只是一个长串,例如响应[1],而不是字典结构。



简而言之,我需要的是数值(我认为这是sting的一部分),所以我可以在我的代码的其余部分测试条件。是字典吗?感谢新的和迷失的

解决方案

你必须使用python语法如下:

 >>>打印响应['swell'] ['components'] ['primary'] ['direction'] 
222.5


A bit lost after much research. My code below parses the JSON to a dictionary I have thought using json load

response = json.load(MSW) # -->Will take a JSON String & Turn it into a python dict

Using the iteration below I return a series like this which is fine

{u'swell': {u'components': {u'primary': {u'direction': 222.5}}}}
{u'swell': {u'components': {u'primary': {u'direction': 221.94}}}}

ourResult = response
for rs in ourResult:
    print rs

But how oh how do I access the 222.5 value. The above appears to just be one long string eg response[1] and not a dictionary structure at all.

In short all I need is the numerical value (which I assume is a part of that sting) so I can test conditions in the rest of my code. Is is a dictionary? With thanks as new and lost

解决方案

You have to use python syntax as follows:

>>> print response['swell']['components']['primary']['direction']
222.5

这篇关于将Python中的JSON解析为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:01
查看更多