Updated on 2024-03-18 GMT+08:00

JSONUtil Embedded Objects

A JSONUtil embedded object provides JSON object methods.

Methods

Table 1 Method description

Method

Description

Example

Object parse(String jsonStr)

Converts a JSON character string into an object.

Assume that variable a is a JSON string. Use the following EL expression to convert the JSON string into an object:

#{JSONUtil.parse(a)}

String toString(Object jsonObject)

Converts an object to a JSON character string.

Assume that variable b is an object. Use the following EL expression to convert the object into a JSON string:

#{JSONUtil.toString(b)}

Object path(String jsonStr,String jsonPath)

Returns the field value in a path specified by the JSON character string. This method is similar to XPath and can be used to retrieve or set JSON by path. You can use . or [] in the path to access members and values. For example, tables[0].table_name.

The content of variable str is as follows:
{
            "cities": [{
                        "name": "city1",
                        "areaCode": "1000"
            },
            {
                        "name": "city2",
                        "areaCode": "2000"
            },
            {
                        "name": "city3",
                        "areaCode": "3000"
            }]
}
The expression for obtaining the area code of city1 is as follows:
#{JSONUtil.path(str,"cities[0].areaCode")}

Example

The content of variable str is as follows:

{
            "cities": [{
                        "name": "city1",
                        "areaCode": "1000"
            },
            {
                        "name": "city2",
                        "areaCode": "2000"
            },
            {
                        "name": "city3",
                        "areaCode": "3000"
            }]
}

The expression for obtaining the area code of city1 is as follows:

#{JSONUtil.path(str,"cities[0].areaCode")}