简体   繁体   English

将 JSON 数组转换为 Object 数组 - Apache 骆驼

[英]Convert JSON array to Object Array - Apache camel

[
  {
    "Name": "ABC", 
    "ID": 1,
    "StartDate": 1444845395112,
    "EndDate": null,
    "ValueAction": null,
    "ValueSource": "lmn"
  }, 
  {
    "Name": "PQR", 
    "ID": 2,
    "StartDate": 1444845395119,
    "EndDate": 1446845395119,
    "ValueAction": null,
    "ValueSource": null
  }, 
  {
    "Name": "XYZ", 
    "ID": 3,
    "StartDate": null,
    "EndDate": null,
    "ValueAction": "qwe",
    "ValueSource": "lmn"
  }
]

How to create object array from this json using split with 'groovy script/java'如何使用 split with 'groovy script/java' 从这个 json 创建 object 数组

[Name, ID, ValueAction, ValueSource] here ValueAction/ValueSource should be null if StartDate is null. So array should be like this. [Name, ID, ValueAction, ValueSource] 如果 StartDate 是 null,这里的 ValueAction/ValueSource 应该是 null。所以数组应该是这样的。

 [[ABC,1, , lmn],[PQR,2, , ],[XYZ,3, , ]]
import groovy.json.*

def json = new JsonSlurper().parseText '''[
  {
    "Name": "ABC",
    "ID": 1,
    "StartDate": 1444845395112,
    "EndDate": null,
    "ValueAction": null,
    "ValueSource": "lmn"
  },
  {
    "Name": "PQR",
    "ID": 2,
    "StartDate": 1444845395119,
    "EndDate": 1446845395119,
    "ValueAction": null,
    "ValueSource": null
  },
  {
    "Name": "XYZ",
    "ID": 3,
    "StartDate": null,
    "EndDate": null,
    "ValueAction": "qwe",
    "ValueSource": "lmn"
  }
]
'''

def res = json.findResults{ it.StartDate ? [ it.Name, it.ID, it.ValueAction ?: '', it.ValueSource ] : null }

assert res.toString() == '[[ABC, 1, , lmn], [PQR, 2, , null]]'

Next time please post the valid JSON.下次请发布有效的 JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM