简体   繁体   English

JSON输出到Python字典-理解困难

[英]JSON Output to Python Dictionary - Difficulty understanding

New to JSON and Python. JSON和Python的新功能。 I successfully call and can get an output using something like this 我成功致电,并可以使用类似这样的输出

def lendbook(currency='xtl'): #GET /lendbook/:currency

    r = requests.get(URL + "/lendbook/" + currency, verify=False)
    rep = r.json()

    return rep

print lendbook()

The data prints out fine but a real muddle for a new beginner. 数据可以很好地打印出来,但是对于新手来说确实是个混乱。 A sample is below from the Python console 以下是来自Python控制台的示例

{u'bids': [{u'timestamp': u'1405112154.0', u'rate': u'0.365', u'frr': u'No', u'amount': u'20.0', u'period': 30}], u'asks': [{u'timestamp': u'1405143214.0', u'rate': u'1.9345', u'frr': u'No', u'amount': u'0.72115369', u'period': 2}, {u'timestamp': u'1401975696.0', u'rate': u'2.0', u'frr': u'Yes', u'amount': u'0.53', u'period': 30}, {u'timestamp': u'1405011490.0', u'rate': u'2.0', u'frr': u'Yes', u'amount': u'3.44796922', u'period': 30}, {u'timestamp': u'1405137594.0', u'rate': u'2.0', u'frr': u'Yes', u'amount': u'0.12279759', u'period': 2}, {u'timestamp': u'1405139396.0', u'rate': u'2.0', u'frr': u'Yes', u'amount': u'1.84883351', u'period': 2}, {u'timestamp': u'1405140302.0', u'rate': u'2.0', u'frr': u'Yes',

Obviously I want to format the date and in fact really only after the rate and period. 显然,我想格式化日期,实际上实际上只是在比率和期间之后。 What looping structure for dictionaries should I use as when I use a FOR loop I only seem to return the words 'bids' and 'asks'. 当我使用FOR循环时,应该使用什么样的字典循环结构,我似乎只返回单词“ bids”和“ asks”。 There is obviously another structure in the dictionary I am not understanding? 字典中显然还有另外一种我不了解的结构?

Really appreciate help as I get my head around JSON and Python dictionaries 当我了解JSON和Python字典时,非常感谢您的帮助

When you loop through a dictionary, you only loop through its keys using basic for loop. 遍历字典时,只能使用基本的for循环遍历其keys

Change that to for key, value in dict.iteritems(): . 将其更改for key, value in dict.iteritems():

>>> obj = {'bids': [1000, 2999], 'asks': [4, 5]}
>>> for key, value in obj.iteritems():
...     print key, value
... 
bids [1000, 2999]
asks [4, 5]
>>> 

Print the type of the data using type(). 使用type()打印数据的类型。 This way you can know the type of data in the json & process it step by step. 这样,您可以知道json中的数据类型并逐步处理它。

Seems like the JSON object you are getting in return contains objects(lists) (ie, "bids", and "asks") So I guess you are confused as to why you aren't accessing the values. 好像您返回的JSON对象包含对象(列表)(即“出价”和“请求”),所以我想您对为什么不访问这些值感到困惑。 I suggest you first get a hold of the object you are interested in (say "bids"); 我建议您先掌握感兴趣的对象(例如“出价”);

bidsList = rep['bids']

As you can see that "bids" is a list of objects([] >> indicates). 如您所见,“出价”是对象列表([] >>表示)。 You can now iterate. 您现在可以迭代。

#[{'timestamp': '1405112154.0', 'rate': '0.365', 'frr': 'No', 'amount': '20.0', 'period': 30}]
>>> bidsList = rep['bids']
>>> for obj in bidsList:
>>>   print obj
... 
{'timestamp': '1405112154.0', 'rate': '0.365', 'frr': 'No', 'amount': '20.0', 'period': 30}

You can get the values like, 您可以获得类似的值,

>>> for obj in bidsList:
>>>   print obj['timestamp']
....
1405112154.0 
>>>

I suggest you create classes named Bids, Asks and any other object type you know is returned in that JSON object of yours. 我建议您创建名为Bids,Asks的类,并且您知道的其他任何对象类型都将在您的JSON对象中返回。 You can then access the object (rep['theObject']) and create instance of Bids(pass the json object to it and get an instance) It will save you from all this as the logic of getting the object would go in the class itself. 然后,您可以访问对象(rep ['theObject'])并创建Bids的实例(将json对象传递给它并获得一个实例),这将使您免于一切,因为获取对象的逻辑将在类中进行本身。 Better design I would say. 我会说更好的设计。

Also(let's just say you want to stick to this approach), if you don't want to hardcode things, say you want to write a code that is independent of what is returned in the JSON. 另外(假设您要坚持这种方法),如果您不想对事物进行硬编码,则说要编写与JSON返回的内容无关的代码。 In that case you can Iterator over the Dict returned ('rep') in your case and for every object that you get in an iteration, you check the type, if it is a dict itself, then you do the same for it (iterator over the keys), or if it is a list, you iterate like I mentioned, otherwise access the values through the keys. 在这种情况下,您可以对情况返回的Dict进行迭代,并针对迭代中获得的每个对象检查类型,如果它本身是dict,则对其进行相同的操作(迭代器(如果是列表),则像我提到的那样进行迭代,否则通过键访问值。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM