smart_dart_json

Creator: coderz1093

Last updated:

0 purchases

TODO
Add to Cart

Description:

smart dart json

介绍 #
JSON 解析工具:

当解析JSON字段内不存在字段时,可返回默认值,不报错
当解析字段类型不对等时,可返回默认值,不报错
(例如:使用 json_serializable 解析,当服务端返回的字段类型为int,我们将该字段声明为string来解析时会报错,使用本库可正常解析成字符串类型)

使用方式参考 #
void main() {
const json =
'{\"title\": \"示例title\", \"data\": [{ \"name\": \"rex\", \"age\": 10 }] }';
final sJson = SDartJson(json);
final title = sJson['title'].stringValue;
final data = sJson['data'].arrayValue.map((e) => e.mapValue).toList();

//模拟未定义的字段获取 (方式一) 可返回默认值
final undefinedKey1 = sJson['undefinedKey1'].stringValue;
//模拟未定义的字段获取 (方式二)
final undefinedKey2 = sJson['undefinedKey2'].string;

print('title : $title');
print('data : $data');
print('undefinedKey1 : $undefinedKey1');
print('undefinedKey2 : $undefinedKey2');
}
copied to clipboard
打印结果
title : 示例title
data : [{name: rex, age: 10}]
undefinedKey1 :
undefinedKey2 : null
copied to clipboard
使用本库进行模型转换可查看 test.dart 文件
Features #
公司老五写的json解析工具类,用习惯了,为了能在自己的开源库里使用上,特此借用。

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.