Giter Site home page Giter Site logo

g_json's Introduction

json Pub Package

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"}

g_json's People

Contributors

kevingong2013 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

g_json's Issues

Example for fetch json from api / URL

hi, thx for this package..

i'm newb on dart/flutter and need more example to do parse json from URL.

i was try like this, but didnt work.

getData() async {
      var result = await http.get(Uri.parse(apiUrl));
      return JSON.parse(result.body);
}

Example's don't work

I'm trying to implement this library but I can't get it to work. Is there a chance you could leave a full example?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.