AntoninoProva
Member
I need to iterate the data in this json code, I tried with a listhashmap but it gives me error. How can I solve it?
This is the code:
import groovy.json.JsonSlurper
import java.util.List
import java.util.HashMap
import com.oracle.e1.common.OrchestrationAttributes
String main(OrchestrationAttributes orchAttr, String input) {
def jsonMap = new JsonSlurper().parseText(input)
// Estrai l'array "latestRates" dal JSON
def jsonArray = jsonMap.latestRates
// Converte l'array JSON in una lista di HashMap
List<HashMap<String, Object>> dataList = []
jsonArray.each { item ->
// Controlla se l'elemento è una mappa
if (item instanceof Map) {
def itemMap = [:] // Crea una nuova mappa per ciascun elemento JSON
// Copia i campi dell'elemento JSON nella mappa
item.each { key, value ->
itemMap[key] = value // Usa il metodo put() per aggiungere i campi
}
// Aggiungi la mappa alla lista
dataList.add(itemMap)
}
}
return dataList;
}
and this is the error:
JSONException A JSONObject text must begin with '{' at character 1 of [{country=AFGHANISTAN, currency=Afghani, isoCode=AFN, uicCode=115, eurRate=83.9676, usdRate=78.8650, usdExchangeConvention=Quantita' di valuta estera per 1 Dollaro, usdExchangeConventionCode=C, referenceDate=2023-09-22}
This is the code:
import groovy.json.JsonSlurper
import java.util.List
import java.util.HashMap
import com.oracle.e1.common.OrchestrationAttributes
String main(OrchestrationAttributes orchAttr, String input) {
def jsonMap = new JsonSlurper().parseText(input)
// Estrai l'array "latestRates" dal JSON
def jsonArray = jsonMap.latestRates
// Converte l'array JSON in una lista di HashMap
List<HashMap<String, Object>> dataList = []
jsonArray.each { item ->
// Controlla se l'elemento è una mappa
if (item instanceof Map) {
def itemMap = [:] // Crea una nuova mappa per ciascun elemento JSON
// Copia i campi dell'elemento JSON nella mappa
item.each { key, value ->
itemMap[key] = value // Usa il metodo put() per aggiungere i campi
}
// Aggiungi la mappa alla lista
dataList.add(itemMap)
}
}
return dataList;
}
and this is the error:
JSONException A JSONObject text must begin with '{' at character 1 of [{country=AFGHANISTAN, currency=Afghani, isoCode=AFN, uicCode=115, eurRate=83.9676, usdRate=78.8650, usdExchangeConvention=Quantita' di valuta estera per 1 Dollaro, usdExchangeConventionCode=C, referenceDate=2023-09-22}