Lazy JSON
通过向JSON对象和JSON数组添加扩展方法,提供null安全实现来简化JSON数据处理。
入门
在pubspec.yml中添加依赖
dependencies:
lazy_json: ^1.0.2
用法
在您的.dart文件中导入库
import 'package:lazy_json/lazy_json.dart';
支持的数据类型
字符串
String类型默认值为 "" (空字符串)
用法
// on JSON object, if 'myKey' not found it returns ''
final myString = myJsonObject.string('myKey');
// on JSON object, if 'myKey' not found it returns 'hello' as default value
final myString = myJsonObject.string('myKey', 'hello');
// on JSON Array, retrieve string value at index 0
final myString = myJsonArray.string(0);
// on JSON Array, retrieve string value at index 3, returns 'hello world' as default value
final myString = myJsonArray.string(3, 'hello world');
整数
int类型默认值为 0 (零)
用法
// on JSON object, if 'myKey' not found it returns 0
final myInteger = myJsonObject.integer('myKey');
// on JSON object, if 'myKey' not found it returns 99 as default value
final myInteger = myJsonObject.integer('myKey', 99);
// on JSON Array, retrieve integer value at index 0
final myInteger = myJsonArray.integer(0);
// on JSON Array, retrieve integer value at index 3, returns 100 as default value
final myInteger = myJsonArray.integer(3, 100);
浮点数 / 双精度
double类型默认值为 0.0 (零)
用法
// on JSON object, if 'myKey' not found it returns 0.0
final myFloat = myJsonObject.float('myKey');
// on JSON object, if 'myKey' not found it returns 99.9 as default value
final myFloat = myJsonObject.float('myKey', 99.9);
// on JSON Array, retrieve float/double value at index 0
final myFloat = myJsonArray.float(0);
// on JSON Array, retrieve float/double value at index 3, returns 100.89 as default value
final myFloat = myJsonArray.float(3, 100.89);
布尔值
bool类型默认值为 false
用法
// on JSON object, if 'myKey' not found it returns false
final myBoolean = myJsonObject.boolean('myKey');
// on JSON object, if 'myKey' not found it returns true as default value
final myBoolean = myJsonObject.boolean('myKey', true);
// on JSON Array, retrieve boolean value at index 0
final myBoolean = myJsonArray.boolean(0);
// on JSON Array, retrieve double value at index 3, returns true as default value
final myBoolean = myJsonArray.boolean(3, true);
JSON对象
Map<String, dynamic>类型默认值为 {} (空对象)
用法
// on JSON object, if 'myKey' not found it returns empty object
final myObject = myJsonObject.integer('myKey');
// on JSON object, if 'myKey' not found it returns JSON object {'a' : 10}
final myObject = myJsonObject.integer('myKey', {'a' : 10});
// on JSON Array, retrieve JSON object at index 0
final myObject = myJsonArray.integer(0);
// on JSON Array, retrieve JSON object at index 3, returns {'b' : 'hello world'} as default value
final myObject = myJsonArray.integer(3, {'b' : 'hello world'});
JSON数组
List<dynamic>类型默认值为 [] (空数组)
用法
// on JSON array, if 'myKey' not found it returns empty array
final myArray = myJsonObject.array('myKey');
// on JSON object, if 'myKey' not found it returns JSON array ['a', 'b']
final myArray = myJsonObject.array('myKey', ['a', 'b']);
// on JSON Array, retrieve JSON array at index 0
final myArray = myJsonArray.array(0);
// on JSON Array, retrieve JSON array at index 3, returns [100, 200, {'b' : 'hello'}] as default value
final myArray = myJsonArray.array(3, [100, 200, {'b' : 'hello'}]);
简写方法
更便捷,所有简写方法本质上都是首字母缩写的方法
myJsonObject.s('myKey'); // shorthand for myJsonObject.string('myKey');
myJsonObject.i('myKey'); // shorthand for myJsonObject.integer('myKey');
myJsonObject.f('myKey'); // shorthand for myJsonObject.float('myKey');
myJsonObject.b('myKey'); // shorthand for myJsonObject.boolean('myKey');
myJsonObject.o('myKey'); // shorthand for myJsonObject.object('myKey');
myJsonObject.a('myKey'); // shorthand for myJsonObject.array('myKey');
myJsonArray.s(1); // shorthand for myJsonArray.string(1);
myJsonArray.i(2); // shorthand for myJsonArray.integer(2);
myJsonArray.f(3); // shorthand for myJsonArray.float(3);
myJsonArray.b(4); // shorthand for myJsonArray.boolean(4);
myJsonArray.o(5); // shorthand for myJsonArray.object(5);
myJsonArray.a(6); // shorthand for myJsonArray.array(6);