Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何拆分包含'_';进入子对象?_Python_Json - Fatal编程技术网

Python 如何拆分包含'_';进入子对象?

Python 如何拆分包含'_';进入子对象?,python,json,Python,Json,我有以下JSON对象,需要在其中对一些标签进行后期处理: { 'id': '123', 'type': 'A', 'fields': { 'device_safety': { 'cost': 0.237, 'total': 22 }, 'device_unit_replacement': { 'cost': 0.262, 'total': 7

我有以下JSON对象,需要在其中对一些标签进行后期处理:

{
 'id': '123',
 'type': 'A',
 'fields': 
  {
      'device_safety': 
      {
         'cost': 0.237,
         'total': 22
      },
      'device_unit_replacement': 
      {
         'cost': 0.262,
         'total': 7
      },
      'software_generalinfo': 
      {
         'cost': 3.6,
         'total': 10
      }
  }
}
我需要按
拆分标签名称以获得以下层次结构:

{
 'id': '123',
 'type': 'A',
 'fields': 
  {
      'device': 
      {
         'safety': 
         {
             'cost': 0.237,
             'total': 22
         },
         'unit':
         {
             'replacement':
             {
                  'cost': 0.262,
                  'total': 7
             }  
         }
      },
      'software': 
      {
         'generalinfo':
         {
            'cost': 3.6,
            'total': 10
         }
      }
  }
}
这是我当前的版本,但我陷入了困境,不知道如何处理字段的层次结构:

import json

json_object = json.load(raw_json)

newjson = {}
for x, y in json_object['fields'].items():
    hierarchy = y.split("_")
    if len(hierarchy) > 1:
         for k in hierarchy:
              newjson[k] = ????

newjson = json.dumps(newjson, indent = 4)

下面是一个递归函数,它将处理一个
dict
并拆分键:

def splitkeys(dct):
    if not isinstance(dct, dict):
        return dct
    new_dct = {}
    for k, v in dct.items():
        bits = k.split('_')
        d = new_dct
        for bit in bits[:-1]:
            d = d.setdefault(bit, {})
        d[bits[-1]] = splitkeys(v)
    return new_dct


>>> splitkeys(json_object)
{'fields': {'device': {'safety': {'cost': 0.237, 'total': 22},
                       'unit': {'replacement': {'cost': 0.262, 'total': 7}}},
            'software': {'generalinfo': {'cost': 3.6, 'total': 10}}},
 'id': '123',
 'type': 'A'}

如果你有
{'A_B_C':{'D':0},'A_B':{'D':1}
?@RiccardoBucco:很好的观点。对于我的原始JSON文件,这种情况不会发生。