简体   繁体   English

尝试使用python字典重新格式化JSON数据

[英]trying to reformat JSON data using python dictionary

I have been trying to format live JSON data coming from an API to be read in Django views. 我一直在尝试格式化来自API的实时JSON数据,以便在Django视图中读取。 However, data coming is little complicated. 但是,数据传入并不复杂。

I have incoming JSON data in format 我有格式的传入JSON数据

    { Time1:
         {'A':'Value',
          'B':'Value',
          }
     Time2:
         {'A':'Value',
          'B':'Value',
          }
there are multiple time records....
}

I need to convert it into 我需要将其转换为

{
  'Time': Time1
  'A'   : 'Value'
  'B'   : 'Value'
},
{
  'Time': Time2
  'A'   : 'Value'
  'B'   : 'Value'
},
{
  'Time': Time3
  'A'   : 'Value'
  'B'   : 'Value'
},
{
  'Time': Time4
  'A'   : 'Value'
  'B'   : 'Value'
},...and so on

Assuming your data is in a proper dict format incoming. 假设您的数据以正确的dict格式传入。 You can do this 你可以这样做

in_json = { 2014: {'A':'Value', 'B':'Value'}, 2015: {'A':'Value', 'B':'Value'}}
newl = list()

for k in in_json:
    x = dict({'Time':k})
    x.update(in_json[2014])
    newl.append(x)

Basically just adding the key from the original dict as a value to a dict and appending it to the list 基本上只是将原始dict的key作为值添加到dict并将其附加到列表

As mentioned in comments, indicated output format is a list. 如评论中所述,指示的输出格式是列表。 In this case you'll just add the new entry for "Time" for each nested dict: 在这种情况下,您只需为每个嵌套字典添加“时间”的新条目:

final_list = []
 for key, subdict in in_dict.iteritems():
     subdict["Time"] = key 
     final_list.append(subdict)

Or if you prefer inline: 或者,如果您更喜欢内联:

final_list = [dict([("Time", key)] + sub.items()) for key, sub in in_dict.iteritems()]

You can use a list comprehension structure, (the code below has been tested using Python3.6 ): 您可以使用列表理解结构(以下代码已使用Python3.6进行了测试):

# Given your JSON has successfully been parsed into a dictionary
> input={'Time1': {'A':'Value1A', 'B':'Value1B'}, 'Time2': {'A','Value2A', 'B', 'Value2B'}}

# Iterate over the dictionary and build a new item out of each key/value couple
> transformed=[(v.update({'Time': k}) or v) for (k, v) in input.items()]

> print(transformed)
[
  {
    'A'   : 'Value1A'
    'B'   : 'Value1B'
    'Time': 'Time1'
  },{
    'A'   : 'Value2A'
    'B'   : 'Value2B'
    'Time': 'Time2'
  }, …
]

What happens with (v.update({'Time': k}) or v) ? (v.update({'Time': k}) or v)什么?
Given v is a dictionary, v.update(...) will add a new entry to the instance (in other words, it mutates the instance). 给定v是字典, v.update(...)将向该实例添加一个新条目(换句话说,它会使实例发生变化)。 This method doesn't return the instance though but None , fortunately wrapping the call between parenthesis gives the opportunity to build an expression that will ultimately return the v instance. 此方法不返回虽然实例,但None ,好在包裹括号之间的调用提供建立,最终会返回一个表达意见的机会v实例。 v being a dict object (given it is not empty) it will be evaluated truthfully in a Boolean context, hence the (… or v) construct. v是dict对象(假设它不为空),它将在布尔值上下文中如实评估,因此(… or v)构造。

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

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