簡體   English   中英

從多個數組列表中准備對象的 JSON 數組

[英]Preparing JSON array of Objects from multiple array list

我對 Groovy 腳本非常陌生,想從下面的 JSON 輸入構建 JSON 輸出。 請幫忙!

我的 JSON 輸入如下所示:

{
  "id":"1222",
  "storageNode": {
    "uuid": "22255566336",
    "properties": {
      "BuinessUnit": [
        "Light",
        "Fan",
        "Watch"
        ],
      "Contact": [
        "abc@gmail.com",
        "fhh@gmail.com"
        ],
      "Location": [
        "Banglore",
        "Surat",
        "Pune"
        ]
    }
  }
}

預期輸出:

[
{
  "BuinessUnit": "Light",
  "Contact": "abc@gmail.com",
  "Location": "Banglore"
},
{
  "BuinessUnit": "Fan",
  "Contact": "fhh@gmail.com",
  "Location": "Surat"
},
{
  "BuinessUnit": "Watch",
  "Contact": "",
  "Location": "Pune"
}
]

請注意,如果任何數組與始終是最后一個的值計數不匹配,在這種情況下,必須填充一個空白值 ( "" )。 可以參考“ BusinessUnit ”對象進行數組大小驗證。

我的代碼如下所示:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*;


def Message processData(Message message) {    
    //Body 
    def body = message.getBody(String.class);    
    def jsonSlurper = new JsonSlurper()
    def list = jsonSlurper.parseText(body)
    String temp


    def BU = list.storageNode.properties.get("BusinessUnit")

    def builder = new JsonBuilder(
        BU.collect {
            [
                BusinessUnit: it
                
            ]
        }
    )
    message.setBody(builder.toPrettyString())
    return message
}

它只返回這個:

[
    {
        "BusinessUnit": "Light"
    },
    {
        "BusinessUnit": "Fan"
    },
    {
        "BusinessUnit": "Watch"
    }
]

現在我將如何添加其他部分? 請幫忙!

我想出了以下將源 JSON 字符串轉換為目標 JSON 字符串的解決方案:

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

def json = '''
{
  "id":"1222",
  "storageNode": {
    "uuid": "22255566336",
    "properties": {
      "BusinessUnit": [
        "Light",
        "Fan",
        "Watch"
        ],
      "Contact": [
        "abc@gmail.com",
        "fhh@gmail.com"
        ],
      "Location": [
        "Banglore",
        "Surat",
        "Pune"
        ]
    }
  }
}
'''

println convert(json)

String convert(String json) {
    def list = new JsonSlurper().parseText(json)
    List<String> units = list.storageNode.properties.BusinessUnit
    List<String> contacts = list.storageNode.properties.Contact
    List<String> locations = list.storageNode.properties.Location
    def result = []
    units.eachWithIndex { unit, int index ->
        result << [
            BusinessUnit: unit,
            Contact     : contacts.size() > index ? contacts[index] : '',
            Location    : locations.size() > index ? locations[index] : '',
        ]
    }
    return new JsonBuilder(result).toPrettyString()
}

我省略了從消息中獲取字符串並將轉換后的 JSON 打包為消息的邏輯。 我希望它能幫助你前進。 如果您需要進一步的幫助,請告訴我。

您可以使用內置的 Groovy 工具,例如transpose()

import groovy.json.*

def json = new JsonSlurper().parseText '''{   "id":"1222",   "storageNode": {     "uuid": "22255566336",     "properties": {       
   "BuinessUnit": [         "Light",         "Fan",         "Watch"         ],      
   "Contact": [         "abc@gmail.com",         "fhh@gmail.com"         ],       
   "Location": [         "Banglore",         "Surat",         "Pune"         ]     }   } }'''

def names = json.storageNode.properties*.key
def values = json.storageNode.properties*.value

int maxSize = values*.size().max()
// pad lists with trainiling spaces
values.each{ v -> ( maxSize - v.size() ).times{ v << '' } }

def result = values.transpose().collect{ tuple -> [ names, tuple ].transpose().collectEntries{ it } }

assert result.toString() == '[[BuinessUnit:Light, Contact:abc@gmail.com, Location:Banglore], [BuinessUnit:Fan, Contact:fhh@gmail.com, Location:Surat], [BuinessUnit:Watch, Contact:, Location:Pune]]'

這段代碼可以處理storageNode.properties下的所有內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM