简体   繁体   English

在Python中读取从JSON字符串解析的嵌套对象

[英]Reading a nested object parsed from a JSON string in Python

I have the following JSON string and it is not as straightforward as I would like. 我有以下JSON字符串,它不是我想要的那么简单。 I am hoping for a bit of help. 我希望能有所帮助。 I need to iterate over each entry comparing it to another set of values that are also in a JSON string (which is structured much easier for me). 我需要遍历每个条目,将其与同样包含在JSON字符串中的另一组值(对我而言,结构更容易)进行比较。 I am looking for a suggested pattern getting the data out and using it in a compare and then if it's the result I want - print it. 我正在寻找一种建议的模式来取出数据并在比较中使用它,然后如果要得到它,请打印它。

The JSON: JSON:

{"success":true,
"message":"",
"result":
     {"buy":
            [
             {"Quantity":2.40078779,"Rate":1.11811290},  
             {"Quantity":0.00085042,"Rate":1.11584924} ....

And so on. 等等。 There aa LOT of values. 有很多价值。 The way I am getting at the data is like this: 我获取数据的方式是这样的:

print ("Buy: ",b1['result']['buy'][0]['Quantity'], ",",b1['result']['buy'][0]['Rate'])

It's the middle [0] this is getting me - I can imagine I could get the number of entries, then loop through i = 0 to range(number of entries) but it really throws my logic off as the other JSON strings with entries I am comparing are structured much easier: 它是中间的[0],这使我得到-我可以想象得到条目的数目,然后通过i = 0循环到range(条目的数目),但这确实使我的逻辑像其他带有条目的JSON字符串比较起来更容易:

example of the easy (for me) JSON entry: 简单(对我而言)JSON条目的示例:

{
    "eth_btc":{
        "asks":[
            [105,1],
            [105.5,15],
            [104.504,0.425],
            [105.505,0.1]
        ],
        "bids":[
            [109,6],
            [113.082,0.46540304],
            [104.91,0.99007913],
            [106.83,0.07832332]
        ]
    }
}


for asks in easyJson['eth_btc']['asks']:
    print (asks)

I need to compare this asks value which is easy to iterate over against the rate value in the above string. 我需要将这个要问的值与上面字符串中的rate值进行比较,以进行比较。 Since the structure is different (and I am learning Python as a hobby) I cannot wrap my head around the best way to iterate through these sets of data structured differently. 由于结构是不同的(并且我正在学习Python是一种业余爱好),所以我无法全神贯注地遍历遍历结构不同的这些数据集的最佳方法。 The datasets will be LARGE, comparing 4000 or so values living in 5 sets of data against each other, ONE BY ONE, to find the just the right pair based on my criteria. 这些数据集将是巨大的,将5组数据中的4000个左右的值一对一地进行比较,以根据我的标准找到最合适的一对。

Should I normalize all of the data FROM JSON into something else, then do my comparisons there? 是否应该将JSON中的所有数据标准化为其他数据,然后在那里进行比较? This is a 5 way compare with thousands of values. 这是与数千个值进行比较的5种方法。

pseudo - code: 伪代码:

If ask1 < bid2 then if volume of ask1 is <= volume of bid2 then do something
   etc - then ask1 vs bid3, and so on..
       ask2 vs bid1, ask2 vs bid3 ...
          ask3 vs bid1, ask3 vs bid2, ask3 vs bid4 ...

Apologies if this isn't clear. 抱歉,如果不清楚。 I am trying to tackle this project head on and I feel like I am brute forcing it and not using the power of Python and I want to learn!!!! 我正在尝试解决这个项目,我觉得我在蛮力地强迫它,没有使用Python的功能,我想学习!!!!

Thanks for any help - direction - anything ! 感谢您的任何帮助-指导-一切!

I have sorted out the first part of the question - how to get at the data I was interested in: 我已经整理出问题的第一部分-如何获取我感兴趣的数据:

import json
b="""{"success":true,"message":"","result":{"buy":[{"Quantity":6.67178734,"Rate":0.01390041},{"Quantity":244.11602480,"Rate":0.01386500},{"Quantity":34.17058305,"Rate":0.01385014},{"Quantity":48.76276808,"Rate":0.01383242},{"Quantity":2.00000000,"Rate":0.01383239},{"Quantity":0.07212509,"Rate":0.01383000},{"Quantity":256.05669876,"Rate":0.01381066},{"Quantity":75.00000000,"Rate":0.01381065},{"Quantity":0.12796429,"Rate":0.01381050},{"Quantity":11.51657190,"Rate":0.01381048},{"Quantity":0.07895698,"Rate":0.01381047},{"Quantity":1.00000000,"Rate":0.01380653},{"Quantity":1.07501432,"Rate":0.01380650},{"Quantity":22.18832778,"Rate":0.01380474}]}}
"""
b1 = json.loads(b)
buy = list(b1['result']['buy'])
for item in buy:
    print ("Rate: ",item['Rate'], "Quantity: ",item['Quantity'])

I needed to create a list for the data I needed - otherwise I could not figure out how to iterate through it cleanly. 我需要为所需的数据创建一个列表-否则我无法弄清楚如何干净地对其进行遍历。 If there is a better way of doing this, please advise. 如果有更好的方法,请告知。 I would rather not take DICT object and cast it into a list, but otherwise the way this data is formatted I couldn't figure out how to cleanly iterate through it. 我宁愿不带DICT对象并将其转换为列表,但是否则,格式化数据的方式我无法弄清楚如何对其进行干净的迭代。

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

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