Created
July 8, 2022 07:56
-
-
Save SuperPenguin/0b85ffc2034f7a19b515c142e9d7cc62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
void main() { | |
final obj = jsonDecode(jsonString) as List<dynamic>; | |
final pingpong = obj // | |
.map((e) => PingResponse.fromJson(e as Map<String, dynamic>)) | |
.toList(); | |
for (final p in pingpong) { | |
print(p.seq); | |
print(p.ttl); | |
print(p.time); | |
print(p.ip); | |
print(''); | |
} | |
} | |
String jsonString = ''' | |
[{"seq":1,"ttl":null,"time":null,"ip":null}, {"seq":2,"ttl":null,"time":null,"ip":null}] | |
'''; | |
class PingResponse { | |
PingResponse({ | |
required this.seq, | |
this.ttl, | |
this.time, | |
this.ip, | |
}); | |
factory PingResponse.fromJson(Map<String, dynamic> json) { | |
final timeObj = (json['time'] as num?)?.toInt(); | |
return PingResponse( | |
seq: (json['seq'] as num).toInt(), | |
ttl: (json['ttl'] as num?)?.toInt(), | |
time: timeObj != null ? Duration(milliseconds: timeObj) : null, | |
ip: (json['ip'] as num?)?.toInt(), | |
); | |
} | |
final int seq; | |
final int? ttl; | |
final Duration? time; | |
final int? ip; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment