Argomento precedente: checkCalendarDateArgomento successivo: convertValueToXml


convertJson

La funzione convertJson consente di convertire una stringa JSON valida in ValueMap. Utilizzare questo metodo per convertire una risposta JSON da un servizio REST in un oggetto ValueMap. Quindi è possibile esplorare e accedere all'oggetto ValueMap con le espressioni standard.

Sintassi

vmResult = convertJson(jsonString)

Argomenti

jsonString (String)

Specifica la stringa JSON da analizzare.

Valore restituito

ValueMap

Rappresentazione ValueMap dei dati contenuti nella stringa JSON che è possibile esplorare o cui si può fare riferimento con espressioni.

Se si inserisce un valore null, viene restituito un valore null senza un'eccezione. Se si inserisce una stringa JSON non valida, il metodo restituisce un valore Null e le eccezioni vengono registrate nei log del server.

Esempio

vmResult = convertJson(Process.jsonString)

Considerare un esempio in cui è stato richiamato un servizio REST. La seguente risposta ricevuta viene archiviata con un nome di variabile restResponse in un set di dati di processo:

{
    "UserName": "pamadmin",
    "age": 25,
    "address": {
        "streetAddress": "CA Technologies, 115, IT Park Area",
        "city": "Hyderabad",
        "state": "AP",
        "postalCode": "500084"
    },
    "phoneNumber": [
        {
            "type": "Office",
            "number": "04066812345"
        },
        {
            "type": "Home",
            "number": "04066854321"
        }
    ]
}

Per accedere ai valori UserName, streetAddress e phoneNumber, scrivere il codice seguente:

// Code starts
// Parse the REST response using convertJson() method and store it in a Process dataset variable named as “resultData”
Process.resultData=convertJson(Process.restResponse);
// Access “UserName” from the resultData varaible 
Process.userName = Process.resultData.UserName;
// Street Address is inside address object hence it will be accessed using the following syntax
Process.streetAddress = Process.resultData.address.streetAddress;
// Phone Number is an array and Office number is stored in the first element, hence index [0] is used.
Process.officePhoneNumber = Process.resultData.phoneNumber[0].number; 
// Code ends