2017-02-23 128 views
2

我想以一种很好的方式打印Json,我想摆脱括号,引号和大括号,并且只使用缩进和行尾来显示json的结构体。在Python中,如何通过删除括号和大括号来打印Json

例如,如果我有一个JSON这样的:

{ 
     "A": { 
      "A1": 1, 
      "A2": 2 
     }, 
     "B": { 
      "B1": { 
       "B11": { 
        "B111": 1, 
        "B112": 2 
       }, 
       "B12": { 
        "B121": 1, 
        "B122": 2 
       } 
      }, 
      "B2": { 
       "B21": [1,2], 
       "B22": [1,2] 
      } 
     }, 
     "C": "CC" 
    } 

我怎么能去除{}和[],我要的是打印JSON:

A: 
    A1: 1 
    A2: 2 
B: 
    B1: 
    B11: 
     B111: 1 
     B112: 2 
    B12: 
     B121: 1 
     B122: 2 
    B2: 
    B21: 1, 2 
    B22: 1, 2 
C: CC 
+3

即输出格式看起来有点像[YAML(http://www.yaml.org/),也许你可以使用? – Blckknght

回答

5

可以加载将json转换成python对象,然后将python对象转换为YAML。另一种解决方案是简单地迭代字典并根据需要进行格式化。

下面是将它转换为YAML的示例。它并不能完全给你想要的东西,但它非常接近。有很多方法可以自定义输出,这仅仅是一个快速黑客展示的总体思路:

import json 
import yaml 

data = json.loads(''' 
    { 
     "A": { 
      "A1": 1, 
      "A2": 2 
     }, 
     "B": { 
      "B1": { 
       "B11": { 
        "B111": 1, 
        "B112": 2 
       }, 
       "B12": { 
        "B121": 1, 
        "B122": 2 
       } 
      }, 
      "B2": { 
       "B21": [1,2], 
       "B22": [1,2] 
      } 
     }, 
     "C": "CC" 
    } 
''') 

print yaml.safe_dump(data, allow_unicode=True, default_flow_style=False) 

这是输出我得到:

A: 
    A1: 1 
    A2: 2 
B: 
    B1: 
    B11: 
     B111: 1 
     B112: 2 
    B12: 
     B121: 1 
     B122: 2 
    B2: 
    B21: 
    - 1 
    - 2 
    B22: 
    - 1 
    - 2 
C: CC 
+0

谢谢布莱恩,这是我想要的。我还有一个问题,有什么办法可以将Json转换成yaml吗?为什么我问是因为将python对象转换为yaml会打乱命令(dict中的键),但似乎Yaml不支持OrderedDict。所以我想我是否可以这样做:python object-> Json然后Json-> yaml为了保持键的顺序。 – yabchexu

+1

@yabchexu:这是否解决您的问题? http://stackoverflow.com/q/6921699/7432 –

+0

是的!非常感谢! – yabchexu

1

如果碰巧你想让它在您最初指定的格式,你可以重载pyyaml类结构根据需要定制:

代码:

import yaml 
from yaml.emitter import Emitter 
from yaml.serializer import Serializer 
from yaml.representer import Representer 
from yaml.resolver import Resolver 

class MyRepresenter(Representer): 

    def represent_sequence(self, tag, sequence, flow_style=None): 
     value = [] 
     node = yaml.SequenceNode(tag, value, flow_style=flow_style) 
     if self.alias_key is not None: 
      self.represented_objects[self.alias_key] = node 
     best_style = True 
     for item in sequence: 
      node_item = self.represent_data(item) 
      if not (isinstance(node_item, yaml.ScalarNode) and 
        not node_item.style): 
       best_style = False 
      value.append(node_item) 
     if best_style: 
      node = self.represent_data(
       str(', '.join('%s' % x.value for x in value))) 
     if flow_style is None: 
      if self.default_flow_style is not None: 
       node.flow_style = self.default_flow_style 
      else: 
       node.flow_style = best_style 
     return node 

class MyDumper(Emitter, Serializer, MyRepresenter, Resolver): 

    def __init__(self, stream, 
      default_style=None, default_flow_style=None, 
      canonical=None, indent=None, width=None, 
      allow_unicode=None, line_break=None, 
      encoding=None, explicit_start=None, explicit_end=None, 
      version=None, tags=None): 
     Emitter.__init__(self, stream, canonical=canonical, 
       indent=indent, width=width, 
       allow_unicode=allow_unicode, line_break=line_break) 
     Serializer.__init__(self, encoding=encoding, 
       explicit_start=explicit_start, explicit_end=explicit_end, 
       version=version, tags=tags) 
     MyRepresenter.__init__(self, default_style=default_style, 
       default_flow_style=default_flow_style) 
     Resolver.__init__(self) 

print yaml.dump(data, Dumper=MyDumper, default_flow_style=False) 

产地:

A: 
    A1: 1 
    A2: 2 
B: 
    B1: 
    B11: 
     B111: 1 
     B112: 2 
    B12: 
     B121: 1 
     B122: 2 
    B2: 
    B21: 1, 2 
    B22: 1, 2 
C: CC