Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

g_json makes it easy to get(set will support in future) value from JSON data in dart2.

License

Notifications You must be signed in to change notification settings

KevinGong2013/g_json

Repository files navigation

json package spirit by SwiftyJSON.

Example

// 0x00 import package
import 'package:g_json/g_json.dart';
...
void someMethod() {
 final jsonStr = '''
 {
 "_id": "5f0fd54e54044a55385a0f31",
 "name": {
 "first": "Coleman",
 "last": "Evans"
 },
 "range": [0, 1, 2, 3, 4, 5, 6,7, 8, 9],
 "friends": [
 {
 "id": 0,
 "name": "Gibson Hull"
 },
 {
 "id": 1,
 "name": "Dunlap Bush"
 },
 {
 "id": 2,
 "name": "Bette Herman"
 }
 ]
 }
 ''';
 // 0x01 parse json string to object
 final mode = JSON.parse(jsonStr);
 final _id = mode['_id'].stringValue;
 final secondFriend = mode[['friends', 2]]['name'].stringValue;
 print(mode.prettyString());
}

Initialization

import 'package:json/json.dart';
final model = JSON(jsonMap);
// or
final model = JSON.parse(jsonString);

Subscript

// Getting a string from a JSON Array
final name = json[0].stringValue;
// Getting an array of string from a JSON Array
final names = json.arrayValue.map((j) => j['name'].stringValue)
// Getting a string using a path to the element
final path = [1, 'list', 2, 'name'];
final name = json[path].string;
// or
final name = json[[1, 'list', 2, 'name']].string;
// Just the same
final name = json[1]['list'][2]['name'].string;

Error

final json = JSON(['name', 'age']);
final name = json[3].string;
if (name != null) {
 // Do something you want
} else {
 print(json[3].error); // List(3) Index is out of bounds.
}
final json = JSON({'name': name, 'age': 20});
final address = json['address'].string;
if (address != null) {
 // Do something you want
} else {
 print(json['address'].error); // "Map["address"] does not exist"
}
final json = JSON.nil;
print(json[0]); // List(0) failure, It is not a List
print(json[0].error); // List(0) failure, It is not a List
print(json['key']); // Map(key) failure, It is not a Map
print(json['key'].error); // Map(key) failure, It is not a Map

Optional getter

// num
final count = json['user']['favourites_count'].num;
if (count != null) {
 // DO something
} else {
 print(json['user']['favourites_count'].error);
}
// String
final name = json['user']['favourites_name'].string;
if (name != null) {
 // DO something
} else {
 print(json['user']['favourites_name'].error);
}
// Bool
final isTranslator = json['user']['is_translator'].bool;
if (isTranslator != null) {
 // DO something
} else {
 print(json['user']['is_translator'].error);
}
...

Non-optional getter

Non=optional getter is named xxxValue

// if not a Number or nil, return 0
int id = json['id'].integerValue;
// if not a string or nil, return ''
String name = json['name'].stringValue;
// if not a list or nil return []
List<JSON> list = json['list'].listValue;
// if not a map or nil, return {}
Map<String, JSON> map = json['map'].mapValue;

Setter

json['name'] = 'G_JSON';
json[0] = 1;

Raw object && convert to jsonString

// raw
final value = json.value;
// json string
final jsonString = json.rawString();

Dynamic member

dynamic person = JSON({'name': 'kevin', 'language': 'dart'});
person.name = 'icey';
person.language = 'Swift';
print(person.rawString()); // {"name": "icey", "language": "swift"}

About

g_json makes it easy to get(set will support in future) value from JSON data in dart2.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

AltStyle によって変換されたページ (->オリジナル) /