Fx.json

Fx.json: APIs related to conversion between json and Map

1. Object to json string

Fx.json.toJson(<Object data>)

Parameter Description

| parameter | type | description |
| ------------ | ------------ | ----------------------- ----------------------------------------- |
| data | Object | object converted to json |

return type

String

Return value description

String

Java example

Fx.json.toJson(Maps.of("a", 1, "b", 2));

Groovy example

Fx.json.toJson(["a" : 1, "b" : 2]) //Return: {"a":1,"b":2}

2. Convert object to json string, specify serialization characteristics

Fx.json.toJson(<Object data>, <SerializerFeature[] features>)

Parameter Description

| parameter | type | description |
| ------------ | ------------ | ----------------------- ----------------------------------------- |
| data | Object | object converted to json |
| features | SerializerFeature[] | variable parameter, list of serialization features |

return type

String

Return value description

String

Java example

Map m = Maps.of("a", 1, "b", null);
log.info(Fx.json.toJson(m));
log.info(Fx.json.toJson(m, SerializerFeature.WriteMapNullValue));

Groovy example

Map m = ["a": 1, "b": null]
log.info(Fx.json.toJson(m))
log.info(Fx.json.toJson(m, SerializerFeature.WriteMapNullValue))

3. Convert json to Map

Fx.json.parse(<String data>)

Parameter Description

| parameter | type | description |
| ------------ | ------------ | ----------------------- ----------------------------------------- |
| data | String | json string |

return type

Map

Return value description

Map

Java example

Map map = Fx.json.parse("{\"a\" : 1, \"b\" : 2}");

Groovy example

Map map = Fx.json.parse("{\"a\" : 1, \"b\" : 2}")

4. Convert json to List

Fx.json.parseList(<String data>)

Parameter Description

| parameter | type | description |
| ------------ | ------------ | ----------------------- ----------------------------------------- |
| data | String | json string |

return type

List

Return value description

List

Java example

List list = Fx.json.parseList("[{\"a\": 1, \"b\": 2},{\"a\": 10, \"b\": 20}]");

Groovy example

List list = Fx.json.parseList('[{"a": 1, "b": 2},{"a": 10, "b": 20}]')

5. Convert json to class

Fx.json.parseObject(<String text>, <Class clazz>)

Parameter Description

| parameter | type | description |
| ------------ | ------------ | ----------------------- ----------------------------------------- |
| text | String | json string |
| clazz | Class | The type that needs to be converted, such as List.class |

return type

T

Return value description

clazz needs to be converted into an object Class

Java example

List list = Fx.json.parseObject("[{\"a\": 1, \"b\": 2},{\"a\": 10, \"b\": 20}]", List .class);

Groovy example

List tmp = Fx.json.parseObject('[{"a": 1, "b": 2},{"a": 10, "b": 20}]', List.class)

2022-11-23
0 0