- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我确实有以下 JSON 对象 -
{
"Resource": [
{
"@name": "Bravo",
"@signature": "h#Bravo",
"@type": "ESX_5.x",
"@typeDisplayName": "ESX Server",
"PerfList": {
"@attrId": "cpuUsage",
"@attrName": "Usage",
"Data": [
{
"@data": "26.00",
"@end": "01:05:00",
"@interval": "60",
"@start": "01:04:00"
},
{
"@data": "24.00",
"@end": "01:04:00",
"@interval": "60",
"@start": "01:03:00"
},
{
"@data": "36.00",
"@end": "01:03:00",
"@interval": "60",
"@start": "01:02:00"
},
{
"@data": "38.00",
"@end": "01:02:00",
"@interval": "60",
"@start": "01:01:00"
},
{
"@data": "37.00",
"@end": "01:01:00",
"@interval": "60",
"@start": "01:00:00"
}
]
},
"Resource": [
{
"@name": "Tango",
"@signature": "vm#Tango",
"@type": "vm",
"@typeDisplayName": "Virtual Machine",
"PerfList": {
"@attrId": "cpuUsage",
"@attrName": "Usage",
"Data": {
"@data": "12.00",
"@end": "04:05:00",
"@interval": "60",
"@start": "04:04:00"
}
}
},
{
"@name": "Charlie",
"@signature": "vm#Charlie",
"@type": "vm",
"@typeDisplayName": "Virtual Machine",
"PerfList": {
"@attrId": "cpuUsage",
"@attrName": "Usage",
"Data": [
{
"@data": "12.00",
"@end": "04:20:00",
"@interval": "60",
"@start": "04:19:00"
},
{
"@data": "12.00",
"@end": "04:19:00",
"@interval": "60",
"@start": "04:18:00"
}
]
}
}
]
},
{
"@name": "Alpha",
"@signature": "h#Alpha",
"@type": "ESX_5.x",
"@typeDisplayName": "ESX Server",
"PerfList": [
{
"@attrId": "cpuUsage",
"@attrName": "Usage",
"Data": {
"@data": "9",
"@end": "06:10:00",
"@interval": "60",
"@start": "06:09:00"
}
},
{
"@attrId": "cpuUsagemhz",
"@attrName": "Usage MHz",
"Data": {
"@data": "479",
"@end": "06:10:00",
"@interval": "60",
"@start": "06:09:00"
}
}
]
}
]
}
我正在寻找一些 JSON 遍历来访问所有键并将上面的转换为以下预期的 python 字典 -
d = { 'ESX_5.x' :
{
'Bravo' :
{
"@typeDisplayName" : "ESX Server",
"@signature" : "h#Bravo",
"cpuUsage" :
{
"from_01:04:00_to_01:05:00" : 26.00,
"from_01:03:00_to_01:04:00" : 24.00,
"from_01:02:00_to_01:03:00" : 36.00,
"from_01:01:00_to_01:02:00" : 38.00,
"from_01:00:00_to_01:01:00" : 37.00,
"interval" : 60
},
"vm" :
{
"Tango" :
{
"@typeDisplayName" : "Virtual Machine",
"@signature" : "vm#Tango",
"cpuUsage" :
{
"from_04:04:00_to_04:05:00" : 12.00,
"interval" : 60
}
},
"Charlie" :
{
"@typeDisplayName" : "Virtual Machine",
"@signature": "vm#Charlie",
"cpuUsage" :
{
"from_04:19:00_to_04:20:00" : "12.00",
"from_04:18:00_to_04:19:00" : "12.00",
"@interval": "60",
}
}
},
},
'Alpha' :
{
"@typeDisplayName" : "ESX Server",
"@signature" : "h#Alpha",
"cpuUsage" :
{
"from_06:09:00_to_06:10:00" : 9,
"@interval": "60"
},
"cpuUsagemhz" :
{
"from_06:09:00_to_06:10:00" : 479,
"@interval": "60"
}
}
}
}
需要递归函数来获取资源和 PerfList 和数据以及自定义字典。
手工制作的预期字典中可能存在拼写错误/语法错误...
到目前为止,这是我的代码 --然而,这对于 N 个嵌套资源是失败的。
import json
class MQLPrettyPrint():
KEY_RESPONSE = 'Response'
KEY_RESULTS = 'Results'
KEY_RESOURCE = 'Resource'
def __init__(self,file=None):
self._json_file = file
self._json_data = self.read_json_file()
self._json_dict = self.json_to_dict()
def json_file(self):
return self._json_file
def read_json_file(self):
json_data = ""
try:
JSON = open(self._json_file,"r")
json_data = JSON.read()
JSON.close()
except:
raise
return json_data
def json_to_dict(self):
return json.loads(self._json_data)
def json_data(self):
return self._json_data
def json_dict(self):
return self._json_dict
def json2mql(self):
for key in self._json_dict:
if key == self.KEY_RESPONSE:
val = self._json_dict[key]
response = self.fetch_response(val)
def fetch_response(self,dict):
for key in dict:
if key == self.KEY_RESULTS:
val = dict[key]
results = self.fetch_results(val)
def fetch_results(self,dict):
for key in dict:
if key == self.KEY_RESOURCE:
val = dict[key]
resource = self.fetch_resource(val)
def fetch_resource(self,resources,dict={}):
if isinstance(resources,list):
for resource in resources:
print "\n\n",resource
if isinstance(resource,__builtins__.dict):
#header = self.fetch_resource_header(resource)
#perfList = self.fetch_perf_list(resource)
self.fetch_resource(resource)
elif isinstance(resources,dict):
header = self.fetch_resource_header(resource)
perfList = self.fetch_perf_list(resource)
else:
print resources
def fetch_resouce_header(resource):
name = resource['@name']
signature = resource['@signature']
type = resource['@type']
typeDisplayName = resource['@typeDisplayName']
resource_dict = {'@name' : name,
'@signature' : signature,
'@type' : type,
'@typeDisplayName' : typeDisplayName}
return resource_dict
def fetch_perf_list(self,resource,perfDict={}):
perfLists = resource['PerfList']
if isinstance(perfLists,list):
for perf in perfLists:
self.fetch_perf_list(perf,perfDict)
elif isinstance(perfLists,dict):
header = self.fetch_perf_header(perf)
dataList = self.fetch_data(perf)
key = ""
if len(perfDict) == 0:
key = header['@attrId']
perfDict[key] = header
perfDict[key]['Data'] = dataList
else:
if not perfDict.has_key(key):
perfDict[key] = header
perfDict[key]['Data'] = dataList
else:
if perfDict.has_key('Data'):
perfDict[key]['Data'].update(dataList)
else:
perfDict[key]['Data'] = dataList
else:
print perfLists
return perfDict
def fetch_perf_header(self,perfDict):
header = {}
attrID = perfDict['@attrId']
attrName = perfDict['@attrName']
header = {'@attrId' : attrID,
'@attrName' : attrName}
return header
def fetch_data(self,perfDict,dataDict={}):
dataList = perfDict['Data']
if isinstance(dataList,list):
for data in dataList:
#Fetch internal data
self.fetch_data(data,dataDict)
elif isinstance(dataList,dict):
start = dataList['@start']
end = dataList['@end']
interval = dataList['@interval']
data = dataList['@data']
key = "%s_%s" % (start,end)
dataDict[key] = dataList
#data_dict = {key : dataList}
#if len(dataDict) == 0:
# dataDict[key] = data_dict
#else:
# dataDict['Data'].update(data_dict)
else:
print dataList
return dataDict
最佳答案
有时在使用递归函数对嵌套结构进行操作时,更容易从行走函数和操作函数的角度来思考。所以我们要针对json结构中包含的所有dict,对其进行转换操作。
在处理嵌套时,就地转换结构比重新创建新结构要容易得多。从该 json 结构构建嵌套字典的更困难的方法涉及能够处理特定的 json 元素,将它们放置在新结构的正确深度和分支中;这涉及两个平行的行走操作。
但要注意的一件事是在遍历嵌套结构时修改嵌套结构,因为转换操作可能会更改遍历函数当前正在迭代的列表。在这种情况下,只有 child (不是 sibling )在走在较低的分支上之前首先被修改。
from copy import deepcopy
import json
from pprint import pprint
from StringIO import StringIO
json_str = \
'''
{
"Resource": [
{
"@name": "Bravo",
"@signature": "h#Bravo",
"@type": "ESX_5.x",
"@typeDisplayName": "ESX Server",
"PerfList": {
"@attrId": "cpuUsage",
"@attrName": "Usage",
"Data": [
{
"@data": "26.00",
"@end": "01:05:00",
"@interval": "60",
"@start": "01:04:00"
},
{
"@data": "24.00",
"@end": "01:04:00",
"@interval": "60",
"@start": "01:03:00"
},
{
"@data": "36.00",
"@end": "01:03:00",
"@interval": "60",
"@start": "01:02:00"
},
{
"@data": "38.00",
"@end": "01:02:00",
"@interval": "60",
"@start": "01:01:00"
},
{
"@data": "37.00",
"@end": "01:01:00",
"@interval": "60",
"@start": "01:00:00"
}
]
},
"Resource": [
{
"@name": "Tango",
"@signature": "vm#Tango",
"@type": "vm",
"@typeDisplayName": "Virtual Machine",
"PerfList": {
"@attrId": "cpuUsage",
"@attrName": "Usage",
"Data": {
"@data": "12.00",
"@end": "04:05:00",
"@interval": "60",
"@start": "04:04:00"
}
}
},
{
"@name": "Charlie",
"@signature": "vm#Charlie",
"@type": "vm",
"@typeDisplayName": "Virtual Machine",
"PerfList": {
"@attrId": "cpuUsage",
"@attrName": "Usage",
"Data": [
{
"@data": "12.00",
"@end": "04:20:00",
"@interval": "60",
"@start": "04:19:00"
},
{
"@data": "12.00",
"@end": "04:19:00",
"@interval": "60",
"@start": "04:18:00"
}
]
}
}
]
},
{
"@name": "Alpha",
"@signature": "h#Alpha",
"@type": "ESX_5.x",
"@typeDisplayName": "ESX Server",
"PerfList": [
{
"@attrId": "cpuUsage",
"@attrName": "Usage",
"Data": {
"@data": "9",
"@end": "06:10:00",
"@interval": "60",
"@start": "06:09:00"
}
},
{
"@attrId": "cpuUsagemhz",
"@attrName": "Usage MHz",
"Data": {
"@data": "479",
"@end": "06:10:00",
"@interval": "60",
"@start": "06:09:00"
}
}
]
}
]
}
'''
def walk_fun_lim(ilist, func=None):
'''
Recursively walk a nested list and dict structure, running func on all dicts
'''
def walk_fun_lim_helper(ilist, func=None, count=0):
tlist = []
ttlist = []
if(isinstance(ilist, list)):
ttlist = filter(lambda x: x, func(filter(lambda x: isinstance(x, dict), ilist)))
if(ttlist):
tlist += ttlist
for q in ilist:
ttlist = filter(lambda x: x, walk_fun_lim_helper(q, func, count+1))
if(ttlist):
tlist += ttlist
elif(isinstance(ilist, dict)):
ttlist = filter(lambda x: x, func([ilist]))
if(ttlist):
tlist += ttlist
for q in ilist:
ttlist = filter(lambda x: x, walk_fun_lim_helper(ilist[q], func, count+1))
if(ttlist):
tlist += ttlist
return [tlist] if(count != 0) else tlist
if(func != None and hasattr(func, "__call__")):
return walk_fun_lim_helper(ilist, func)
else:
return []
def transformers_robots_in_disguise(x):
for idict in x:
plist = idict.pop("PerfList", [])
plist = plist if(isinstance(plist, list)) else [plist]
for sub_dict in plist:
sub_name = sub_dict.pop("@attrId")
dlist = sub_dict.pop("Data", [])
dlist = dlist if(isinstance(dlist, list)) else [dlist]
new_dict = {}
for sub_dict in dlist:
new_dict["from_%(@start)s_to_%(@end)s" % sub_dict] = sub_dict["@data"]
new_dict["@interval"] = sub_dict["@interval"]
idict[sub_name] = new_dict
rlist = idict.pop("Resource", [])
rlist = rlist if(isinstance(rlist, list)) else [rlist]
for sub_dict in rlist:
sub_type = sub_dict.pop("@type")
sub_name = sub_dict.pop("@name")
idict.setdefault(sub_type, {})[sub_name] = sub_dict
return []
json_data = json.load(StringIO(json_str))
data_copy = deepcopy(json_data)
walk_fun_lim(data_copy, transformers_robots_in_disguise)
pprint(data_copy)
关于python - 复杂的 Python JSON 对象到自定义字典的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17600833/
我之前发布过question已得到答复,但我也需要对此进行查询。我有一个包含这样数据的表结构(日期格式为 dd/mm/yyyy)。 ID Account Number Unit Ad
我正在使用 React Native Calendars 并尝试为议程组件构建我的数据。 预期的数据结构是(一个对象) { '2012-05-22': [{text: 'item 1 - any j
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
两列城镇和优先级。 我需要对表进行排序,以便优先级=1的城镇排在第一位,并且不按名称 ASC 排序,而其余城镇则按名称 ASC 排序。 我该怎么做? 谢谢;) 更新 SELECT * FROM map
我有三个表“Hardware_model”、“Warehouse”和“Brand”,并且表以这种方式一起引用:Hardware_model 仓库Hardware_model 品牌 现在我要执行以下
我有一个 MySQL 表 (tbl_filters),包含 3 列:id、cat、val id 和 val 是数字,cat 是 varchar。每个 id 有多行。 我还有另一个包含多个列的表 (tb
我想获取字段的不同值,比方说:field1...这需要一个如下查询:“从表中选择不同的(字段1)” 但是,对于某些记录,field1 为空,并且还有另一列可以替代 field1,即 field2。对于
表 1 - 用户 id username items 1 Paul 1(0020);2(0001); 表 2 - 项目 id name 1 name_here 在我的用户的项目中,我输入了 2(000
我想连接同一个表 4 次以获取列的显示方式,我不确定是否可以在 1 个 SQL 语句中完成。 tbl_用户名 id username 1 Adam 2 Bob 3 Chris tbl_机
首先,我刚刚开始自己学习JS,没有任何编程经验,这意味着我仍然要了解这种出色的编程语言的基本构建模块。 我的问题与我编写的以下代码有关: let orderCount = 0; con
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我正在使用 XMAPP,MySQL 正在正常运行。在 phpMyAdmin 中,我不太明白这一点,所以我尝试在 PHP 中创建一个。使用此代码,它会告诉我数据库 benutzer。尽管我在 phpMy
是否有一种高效的算法可以找到平均度最大的子图(可能是图本身)? 最佳答案 The paper "Finding a Maximum-Density Subgraph" by Andrew Goldbe
目录 1、业务背景 2、场景分析 3、流程设计 1、业务流程 2、导入流程
我有 2 个表: 1) 包含自 1900 年 1 月 1 日以来所有日期的 Masterdates 表 2) Stockdata 表,其中包含表单中的股票数据 日期、交易品种、开盘价、最高价、最低价、
我有一个非常复杂的 UI,其状态栏不断变化,其中包含多种类型的状态消息,并且 UI 具有复杂的图表控件和已加载的指示性地理 map 。 现在这些小而复杂的区域的数据上下文具有同样复杂的 ViewMod
有人可以用简单的方式向我解释为什么常量在大 O 表示法中无关紧要吗?为什么添加常量时复杂性保持不变。这不是作业问题,我只是想更好地理解这一点。让我明白这个大 O 是为了看到一个函数在接近无穷大时的行为
我在 flex 搜索索引中有以下文档。 [{ "_index": "ten2", "_type": "documents", "_id": "c323c
我有一个以零碎的方式构建的 LINQ 查询,如下所示: var initialQuery = from item in MyContext where xxx == yyy select item;
我目前正在涉足 SQL,并且希望针对我所创建的问题获得一些帮助。 为了练习一些编程,我正在制作一个 IOU 应用程序。下面是我存储的表我的借条记录(忽略一些相关栏目)。该表允许用户说“嘿,你欠我 X
我是一名优秀的程序员,十分优秀!