Groovy:将Json转换为文本

Groovy:将Json转换为文本,groovy,Groovy,希望使用Groovy将下面的json记录转换为文本 import groovy.json.* def js = """{ "title": { "titleid": "222", "titlename": "ABCD", "titledesc": null }, "customer": { "customerDetail": { "customerid": 878378743, "customerstatus": "ACTIV

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

以下代码:

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中没有某个路径的值时记录一些错误,否则将进行处理

还应该注意,上面的代码依赖于这样一个事实:groovy中的映射文字(即
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    

其中,
表示该块与上述示例相同

你能不能加上你试过的和你犯过的错误。
.collect{k,v->k}
.keySet()
(不用担心,它是订购的)是的,为什么我不买它,因为它是一个集合,所以不能保证订购。在LinkedHashMap.keySet()方法中,我没有看到任何关于排序的内容……能告诉我排序的保证来自哪里吗?我可以看到运行时
keySet()
的返回值是
LinkedHashMap$LinkedKeySet
的instanceof,但这并不完全构成对我的公共保证。文档中的第一句话:->顺序是插入顺序。同样,对条目进行迭代也是一样的——因此,按照这种逻辑,您可能无法按照您已经在做的方式来做。