代码之家  ›  专栏  ›  技术社区  ›  1pluszara

Groovy:将Json转换为文本

  •  0
  • 1pluszara  · 技术社区  · 5 年前

    希望使用Groovy将下面的json记录转换为文本

    import groovy.json.*
    
    def js = """{
      "title": {
        "titleid": "222",
        "titlename": "ABCD",
        "titledesc": null
      },
      "customer": {
        "customerDetail": {
          "customerid": 878378743,
          "customerstatus": "ACTIVE",
          "customersystems": {
            "customersystem1": "SYS01",
            "customersystem2": null
          },
          "sysid": null
        },
        "store": {
          "storeid": "LOS002",
          "storename": "LAStore",
          "areacode": "JDHJ8K988"
        },
        "persons": {
          "person1": {
            "personid": "123",
            "personname": "IIISKDJKJSD"
          },
          "person2": {
            "personid": "456",
            "personname": "IUDFIDIKJK"
          }
        },
        "order": {
          "orderdetail": {
            "orderid": "4291026",
            "ordername": "ORD93999"
          }
        },
        "product": {
          "orderdate": "20190101",
          "currency": "USD",
          "amount": 1000.23
        }
      }
    }  
    """
    
    def data = new JsonSlurper().parseText(js)  
    

    customerId,customerstatus,customersystem1,sysid,storeid,storename,person1.personid,person1.personname,orderid,orderdate,currency,amount,titlename
    878378743,ACTIVE,SYS01,null,LOS002,LAStore,123,IIISKDJKJSD,4291026,20190101,USD,1000.23
    

    这只是一个json记录,所以我如何使用Groovy转换所有json记录?

    0 回复  |  直到 5 年前
        1
  •  2
  •   Matias Bjarland    5 年前

    以下代码:

    import groovy.json.*
    
    def js = """
    [
    {
      "title": {
        "titleid": "222",
        "titlename": "ABCD",
        "titledesc": null
      },
      "customer": {
        "customerDetail": {
          "customerid": 878378743,
          "customerstatus": "ACTIVE",
          "customersystems": {
            "customersystem1": "SYS01",
            "customersystem2": null
          },
          "sysid": null
        },
        "store": {
          "storeid": "LOS002",
          "storename": "LAStore",
          "areacode": "JDHJ8K988"
        },
        "persons": {
          "person1": {
            "personid": "123",
            "personname": "IIISKDJKJSD"
          },
          "person2": {
            "personid": "456",
            "personname": "IUDFIDIKJK"
          }
        },
        "order": {
          "orderdetail": {
            "orderid": "4291026",
            "ordername": "ORD93999"
          }
        },
        "product": {
          "orderdate": "20190101",
          "currency": "USD",
          "amount": 1000.23
        }
      }
    }
    ]  
    """
    
    /* 
    customerId,customerstatus,customersystem1,sysid,storeid,storename,person1.personid,person1.personname,orderid,orderdate,currency,amount,titlename
    878378743,ACTIVE,SYS01,null,LOS002,LAStore,123,IIISKDJKJSD,4291026,20190101,USD,1000.23
    */
    
    def data = new JsonSlurper().parseText(js)  
    def mappings = [ 
      customerId:            { n -> n.customer.customerDetail.customerid }, 
      customerstatus:        { n -> n.customer.customerDetail.customerstatus }, 
      customersystem1:       { n -> n.customer.customerDetail.customersystems.customersystem1 }, 
      sysid:                 { n -> n.customer.customerDetail.sysid }, 
      storeid:               { n -> n.customer.store.storeid }, 
      storename:             { n -> n.customer.store.storename }, 
      'person1.personid':    { n -> n.customer.persons.person1.personid }, 
      'person1.personname':  { n -> n.customer.persons.person1.personname }, 
      orderid:               { n -> n.customer.order.orderdetail.orderid }, 
      orderdate:             { n -> n.customer.product.orderdate }, 
      currency:              { n -> n.customer.product.currency }, 
      amount:                { n -> n.customer.product.amount }, 
      titlename:             { n -> n.title.titlename } 
    ]
    
    def headers = mappings.keySet().join(',') //edited thanks to comment
    
    println headers
    data.each { item ->
      def row = mappings.collect { k, v -> v(item) }.join(',')
      println row
    }
    

    按你的要求做。请注意,我将json设置为一个条目列表,而不是单个条目,因为从您的文本中可以看出,这正是您所追求的。

    运行上述代码会产生:

    ~> groovy solution.groovy
    customerId,customerstatus,customersystem1,sysid,storeid,storename,person1.personid,person1.personname,orderid,orderdate,currency,amount,titlename
    878378743,ACTIVE,SYS01,null,LOS002,LAStore,123,IIISKDJKJSD,4291026,20190101,USD,1000.23,ABCD
    ~> 
    

    注意,如果这是进入某个关键系统的,并且不仅仅是一次性的特别代码,那么您可能应该做一些事情,比如检查 v(item) 当json中某个路径没有值时,记录otherwise handle的一些错误。

    还应该注意的是,上面的代码依赖于groovy中的map文本(即。 def mappings = [:] )创建java的LinkedHashMap实例,该实例具有可预测的迭代顺序,例如 keySet() collect { } .

    <<编辑>>

    对于单个项目json blob,您将按如下方式更改代码:

    def js = """
    {
    ...
    }
    """
    
    def item = new JsonSlurper().parseText(js) 
    def mappings = ...
    
    def headers = mappings.keySet().join(',') //edited thanks to comment
    println headers
    
    def row = mappings.collect { k, v -> v(item) }.join(',')
    println row    
    

    ... 表示块与上面的示例保持不变。