開發過程當中,json是必不可少的基礎技能之一。這裏記錄下,在Dart語言中,如何將json解析成實例對象,以及如何將實例對象轉化成json字符串。json
這裏使用的工具是dart:convert
包。bash
咱們的目的很簡單,待解析的json字符串格式以下:ide
{
"key":"wangdandan",
"value":"王蛋蛋的father"
}
複製代碼
class JsonModelDemo {
String key;
String value;
}
複製代碼
咱們建立一個實例對象,而後給這個實例對象賦值,接着使用jsonDecode
方法解析實例對象。代碼以下,工具
import 'dart:convert';
import 'package:dart_demo1/json/json_model.dart';
/// 將實體類對象解析成json字符串
String generatePlatformJson({String key, String value}) {
JsonModelDemo jsonModelDemo = new JsonModelDemo();
jsonModelDemo.key = key;
jsonModelDemo.value = value;
String jsonStr = jsonEncode(jsonModelDemo);
return jsonStr;
}
/// 這裏寫測試方法
main() {
String result1 = generatePlatformJson(key: "result1", value: "result1Value");
print('result1:$result1');
}
複製代碼
執行代碼,報錯以下:測試
lib/json/json_parse_util.dart:1: Warning: Interpreting this as package URI, 'package:dart_demo1/json/json_parse_util.dart'.
Unhandled exception:
Converting object to an encodable object failed: Instance of 'JsonModelDemo'
#0 _JsonStringifier.writeObject (dart:convert/json.dart:645:7)
#1 _JsonStringStringifier.printOn (dart:convert/json.dart:832:17)
#2 _JsonStringStringifier.stringify (dart:convert/json.dart:817:5)
#3 JsonEncoder.convert (dart:convert/json.dart:253:30)
#4 JsonCodec.encode (dart:convert/json.dart:164:45)
#5 jsonEncode (dart:convert/json.dart:76:10)
#6 generatePlatformJson (package:dart_demo1/json/json_parse_util.dart:10:20)
#7 main (package:dart_demo1/json/json_parse_util.dart:16:20)
#8 _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:300:19)
#9 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
Process finished with exit code 255
複製代碼
查找Converting object to an encodable object failed: Instance of 'xxx'
這個錯誤,在stackoverflow上找到答案:stackoverflow.com/questions/2…ui
咱們給model實體類添加toJson方法:this
class JsonModelDemo {
String key;
String value;
/// jsonDecode(jsonStr) 方法中會調用實體類的這個方法。若是實體類中沒有這個方法,會報錯。
Map toJson() {
Map map = new Map();
map["key"] = this.key;
map["value"] = this.value;
return map;
}
}
複製代碼
此次再運行代碼,解析成功,輸出以下:spa
result1:{"key":"result1","value":"result1Value"}
複製代碼
解析代碼以下:code
/// 將json字符串解析成實體類對象
JsonModelDemo parsePlatformJson(String jsonStr) {
JsonModelDemo result = jsonDecode(jsonStr);
return result;
}
複製代碼
測試代碼以下:orm
JsonModelDemo modelDemo = parsePlatformJson(result1);
print('parsePlatformJson:$modelDemo');
複製代碼
爲了方便測試,在JsonModelDemo中重寫toString方法;
@override
String toString() {
return 'JsonModelDemo{key: $key, value: $value}';
}
複製代碼
運行代碼,報錯以下:
Unhandled exception:
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'JsonModelDemo'
#0 parsePlatformJson (package:dart_demo1/json/json_parse_util.dart:16:17)
#1 main (package:dart_demo1/json/json_parse_util.dart:25:29)
#2 _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:300:19)
#3 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
複製代碼
仔細觀察報錯,發現是類型不匹配,具體緣由是jsonDecode方法返回的是Map<String, dynamic>,不是咱們指望的實例對象。因此,咱們還須要將Map<String, dynamic>轉化爲咱們想要的實例對象。
在model中添加轉化方法:
/// jsonDecode(jsonStr)方法返回的是Map<String, dynamic>類型,須要這裏將map轉換成實體類
static JsonModelDemo fromMap(Map<String, dynamic> map) {
JsonModelDemo jsonModelDemo = new JsonModelDemo();
jsonModelDemo.key = map['key'];
jsonModelDemo.value = map['value'];
return jsonModelDemo;
}
複製代碼
接着修改解析方法:
/// 將json字符串解析成實體類對象
JsonModelDemo parsePlatformJson(String jsonStr) {
JsonModelDemo result = JsonModelDemo.fromMap(jsonDecode(jsonStr));
return result;
}
複製代碼
運行代碼,解析成功,輸出以下:
result1:{"key":"result1","value":"result1Value"}
parsePlatformJson:JsonModelDemo{key: result1, value: result1Value}
複製代碼
參考: