将JSon转换为必需的Json Formate

时间:2017-06-14 19:27:04

标签: python json dictionary

我在将JSON转换为所需格式时遇到问题,任何帮助都会受到欢迎希望有人可以提供帮助

这是我的Raw JSON结果:

   //for Time being I have given a single object here, in the same way, I would get 2 more objects in the same JSON response

   OrderedDict([(u'totalSize', 3), (u'done', True), (u'records', 
   [OrderedDict([(u'attributes', OrderedDict([(u'type', u'Site'),
    (u'url', u'/services/data/v29.0/sobjects/Site/0DM30000000CaYgGAK')])), 
    (u'Name', u'Renewals_Community1'), (u'DailyBandwidthLimit', 40960), 
   (u'DailyBandwidthUsed', 0.0), (u'DailyRequestTimeLimit', 3600), 
   (u'DailyRequestTimeUsed', 0.0), (u'MonthlyPageViewsEntitlement', 0)]), 

必需的JSon Formate:

           {
            "DailyBandwidthLimit":
             {
              "Max"://here my own value which is standard
              "Remaining":"40960"//(u'DailyBandwidthLimit', 40960), 
             }
           }

使用转换方法:

编写for循环以仅打印所需数据

      for i in rs['records']:  
    printi['DailyBandwidthLimit']
    ///because of less space I am not placing  all the code

这里我能够获得必填字段但无法形成JSON尝试了不同的选项,例如将数据打印到文件json.dumps&再次使用JSON.loads从文件中获取数据似乎没有任何效果

 Finally
 data[] 
 data['Max'] = 8
 data['Remainig']= d
 data['DailyBandwidthLimit'] = [""]
 json_data = json.dumps(data)
 print  json_data

它在某种程度上,但我无法制作所需的JSON

提前致谢

1 个答案:

答案 0 :(得分:0)

import json

data=dict()
data['DailyBandwidthLimit'] = dict()
data['DailyBandwidthLimit']['Max'] = 8
data['DailyBandwidthLimit']['Remaining'] = 40960

json_data = json.dumps(data)

在屏幕上打印json_data:

print(json.dumps(data, indent=4, sort_keys=True))