Groovy:将Json转换为文本

时间:2019-05-06 04:40:35

标签: groovy

想使用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记录?

1 个答案:

答案 0 :(得分:2)

以下代码:

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等中的某个路径没有值时处理。

还应注意,以上代码依赖于以下事实:常规的地图文字(即def mappings = [:])创建了Java的LinkedHashMap实例,该实例对诸如keySet()和{ {1}}。

<<编辑>>

对于单个项json blob,您将更改代码,如下所示:

collect { }

其中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 表示该块与上面的示例相同。