json_response 2.1.0 copy "json_response: ^2.1.0" to clipboard
json_response: ^2.1.0 copied to clipboard

outdated

A most easily usable JSON response wrapper library in Dart. With JsonResponse, you can easily and safely handle JSON response on your application.

A most easily usable JSON response wrapper library in Dart!

GitHub Gmail Line Twitter

pub package codecov Analyzer Test

1. About #

JsonResponse is an open-sourced Dart library.
With JsonResponse, you can easily and safely handle JSON response on your application.

This library was created with the goal of making JSON response easier, more intuitive, and safer to use in the Dart language. For example, as a result of the communication process with the Web API, JSON is returned from the http package and you have ever written the following process when the JSON is set to Response, right?

void main() async {
  final response = await http.get(Uri.parse('something'));
  final json = jsonDecode(response.body);
  print(json['key'] ?? '');
}

The above process is not only redundant, but also unsafe from an implementation standpoint, as it requires writing a process for when the value associated with the key does not exist. It becomes even more complicated in the case of a list structure with multiple JSONs.

With JsonResponse, the above implementation is no longer necessary!

1.1. Introduction #

1.1.1. Install Library #

With Dart:

 dart pub add json_response

With Flutter:

 flutter pub add json_response

1.1.2. Import It #

import 'package:json_response/json_response.dart';

1.1.3. Use JsonResponse #

import 'package:http/http.dart';
import 'package:json_response/json_response.dart';

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

  final jsonArrayResponse = Response(
    '''[
        {"key1": "value", "key2": 1, "key3": true},
        {"key1": "value", "key2": 1, "key3": true},
        {"key1": "value", "key2": 1, "key3": true}
      ]
    ''',
    200,
  );

  // Json represents a single JSON structure,
  // and the JsonArray class represents a multiple JSON structure.
  //
  // Instantiation of either class is very simple,
  // just pass the Response class returned when HTTP communication is
  // performed with the http package.
  final json = Json.from(response: jsonResponse);
  final jsonArray = JsonArray.from(response: jsonArrayResponse);

  // Intuitively and safely retrieve data from dedicated methods
  // that correspond to data types.
  print(json.getString(key: 'key1'));
  print(json.getInt(key: 'key2'));
  print(json.getBool(key: 'key3'));

  // You can also easily retrieve JSON that is nested within JSON.
  print(json.get(key: 'key4'));
  // You can get this json as a map format.
  print(json.toMap());

  // The forEach method makes it easy to handle repetitive processes.
  jsonArray.forEach((json) {
    print(json);
  });

  // If you are iterating and want the current index as well,
  // the enumerate method is useful.
  jsonArray.enumerate((index, json) {
    print(index);
    print(json);
  });

  // JSON can be retrieved by specifying a specific index,
  // but be aware that an exception will be thrown
  // if a non-existent index number is specified.
  print(jsonArray.get(index: 0));

  // If you don't like the structure of several nested lists,
  // you can use the toFlat method to make the nested structure flat.
  // This method returns a new flattened JsonArray.
  print(jsonArray.toFlat());
}

1.2. License #

Copyright (c) 2021, Kato Shinya. All rights reserved.
Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.

1.3. More Information #

JsonResponse was designed and implemented by Kato Shinya.

4
likes
0
pub points
72%
popularity

Publisher

unverified uploader

A most easily usable JSON response wrapper library in Dart. With JsonResponse, you can easily and safely handle JSON response on your application.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

http

More

Packages that depend on json_response