Json class abstract

This abstract class represents the data structure of JSON, and provides convenient, easy, and safe features for handling JSON.

Instantiation of Json class is very simple, just pass the Response class returned when HTTP communication is performed with the http package.

It also provides methods to easily and safely retrieve values from JSON object.

For example, if you want to retrieve a string associated with a specific key, use the getString method. If this key does not exist or if the value is null, a non-null value set as the default value in each method will be returned. If you want to set a specific default value, you can set the default value as an argument of each method.

If the values associated with a specific key are structured as a list, you can use getStringValues for example. These methods will retrieve all the values in the list associated with the specified key and return them as a list.

You can use use get if the value associated with a specific key is a JSON object represented by a map. The type returned by this method is Json, the same as this class. Even if the value associated with this specific key is a JSON object expressed as a string, no special procedure is required, and you can simply call the get method.

Also you can use getArray if the values associated with a specific key are multiple JSON objects. See the JsonArray class documentation for details.

Example:

void main() {
  final response = Response(
    '{"key1": "value", "key2": 1, "key3": true, "key4": {"nested_key1": "nested_value"}}',
    200,
  );

  final json = Json.from(response: response);

  print(json.getString(key: 'key1'));
  print(json.getInt(key: 'key2'));
  print(json.getBool(key: 'key3'));

  print(json.get(key: 'key4'));
  print(json.toMap());
}

Constructors

Json.empty()
Returns the new instance of empty Json.
factory
Json.from({required Response response})
Returns the new instance of Json based on response.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
isEmpty bool
Returns true if this json object is empty, otherwise false.
no setter
isNotEmpty bool
Returns true if this json object is not empty, otherwise false.
no setter
keySet Set<String>
Returns the key set of this json object.
no setter
length int
Returns the length of this json.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

containsKey({required String key}) bool
Returns true if json contains key associated with key passed as an argument.
forEach(void action(String key, dynamic value)) → void
Applies action to each key/value pair of the json.
get({required String key}) Json
Returns the child json associated with the key, otherwise empty json object.
getArray({required String key}) JsonArray
Returns the child json array associated with the key, otherwise empty json array.
getBool({required String key, bool defaultValue = false}) bool
Returns the bool value associated with the key, otherwise defaultValue.
getBoolValues({required String key}) List<bool>
Returns the bool value list associated with the key, otherwise empty list.
getDouble({required String key, double defaultValue = -1.0}) double
Returns the double value associated with the key, otherwise defaultValue.
getDoubleValues({required String key}) List<double>
Returns the double value list associated with the key, otherwise empty list.
getInt({required String key, int defaultValue = -1}) int
Returns the int value associated with the key, otherwise defaultValue.
getIntValues({required String key}) List<int>
Returns the int value list associated with the key, otherwise empty list.
getString({required String key, String defaultValue = ''}) String
Returns the string value associated with the key, otherwise defaultValue.
getStringValues({required String key}) List<String>
Returns the string value list associated with the key, otherwise empty list.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toMap() Map<String, dynamic>
Returns this json as a map format.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited