使用 Django 从 json 获取相应的时间和值 (Python 2)

Get corresponding time and value from json using Django (Python 2)

以下是我从源中获取的 JSON 示例。我需要从中获取某些值,例如时间、值、值类型。时间和值在不同的数组和不同的深度。

所以,我的目标是获取时间和值,例如值 1,我应该获取时间戳 15158315880002 15158387880003 对于 1515845987000.

请注意,这只是一个示例JSON,因此请忽略我在编写此数组时可能犯的任何错误。

{
  "feed":{
  "component":[
     {
        "stream":[
           {
              "statistic":[
                 {
                    "type":"DOUBLE",
                    "data":[
                       1,
                       2,
                       3
                    ]
                 }
              ],
              "time":[
                 1515831588000,
                 1515838788000,
                 1515845987000
              ]
           }
        ]
     }
  ]
 },
"message":"",
"success":true
}

这是我编写的一个函数,用于获取值,但我在最后一步获得的时间戳不正确。请帮忙解决这个问题。

get_feed_data() 是给出上述 JSON.

的函数
# Fetch the component UIDs from the database
component_uids = ComponentDetail.objects.values('uid')
# Make sure we have component UIDs to begin with
if component_uids:
    for component_uid in component_uids:
        # Fetch stream UIDs for each component from the database
        stream_uids = StreamDetail.objects.values('uid').filter(comp_id=ComponentDetail.objects.get(uid=component_uid['uid']))
        for stream_uid in stream_uids:
            feed_data = json.loads(get_feed_data(component_uid['uid'], stream_uid['uid']))
            sd = StreamDetail.objects.get(uid=stream_uid['uid'])
            for component in feed_data['feed']['component']:
                for stream in component['stream']:
                    t = {}
                    d = {}
                    stats = {}
                    for time_value in stream['time']:
                        t = time_value
                    for stats in stream['statistic']:
                        for data_value in stats['data']:
                            print({
'uid': sd, value_type:stats['type'], timestamp=t, value=data_value
})

我终于用列表搞定了。我只是将数据附加到一个列表中,稍后使用索引在循环中调用它并获取相应的日期和时间。