'Parsing Nested JSON give Unhandled Exception
I am trying to parse complex Nested JSON, I have multiple classes to get down the JSON nests. I am looking for the numbers from this JSON
{
"data": {
"attributes": {
"last_analysis_stats": {
"harmless": 81,
"malicious": 2,
"suspicious": 0
}
}
}
}
but whenever I parse it, I get the error "Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Data'"
the code that I am using is below
_GoneSmishinState goneSmishinStateFromJson(String str) => _GoneSmishinState.fromJson(json.decode(str));
String goneSmishinStateToJson(_GoneSmishinState data) => json.encode(data.toJson());
String url = "https://urlhaus-api.abuse.ch/v1/urls/recent/"; //address for URL file
int harmless = 0;
Future<void> main() async {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key:key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: "Gone Smishin'",
home: GoneSmishin(),
);
}
}
class Data {
late Attributes attributes;
Data.fromJson(Map<String, dynamic> json)
: attributes = Attributes.fromJson(json["attributes"]);
Map<String, dynamic> toJson() => {
"attributes": attributes.toJson(),
};
}
class Attributes {
late LastAnalysisStats lastAnalysisStats;
Attributes.fromJson(Map<String, dynamic> json)
: lastAnalysisStats= LastAnalysisStats.fromJson(json["last_analysis_stats"]);
Map<String, dynamic> toJson() => {
"last_analysis_stats": lastAnalysisStats.toJson(),
};
}
class LastAnalysisStats {
late int harmless;
late int malicious;
late int suspicious;
LastAnalysisStats.fromJson(Map<String, dynamic> json)
: harmless= json["harmless"],
malicious= json["malicious"],
suspicious= json["suspicious"];
Map<String, dynamic> toJson() => {
"harmless": harmless,
"malicious": malicious,
"suspicious": suspicious,
};
}
String message = "";
String word = "";
bool isOn = false;
@override
void dispose() {
myController.dispose();
//super.dispose();
}
var data = '';
var attributes = '';
String virusTotal = "VirusTotal";
String list = "Whitelist";
final myController = TextEditingController();
class GoneSmishin extends StatefulWidget {
const GoneSmishin({Key? key}) : super(key: key);
@override
_GoneSmishinState createState() => _GoneSmishinState();
}
class _GoneSmishinState extends State<GoneSmishin> {
_GoneSmishinState({Key? key}) : super();
late Data data;
_GoneSmishinState.fromJson(Map<String, dynamic> json)
: data= Data.fromJson(json["data"]);
Map<String, dynamic> toJson() => {
"data": data.toJson(),
};
urlHausParseBox() async {
String url = myController.text;
var urlEncoded = base64.encode(utf8.encode(myController.text));
var urlNoPadding = urlEncoded.replaceAll(new RegExp(r'='), '');
final response2 = await http.get(
Uri.parse("https://www.virustotal.com/api/v3/urls/$urlNoPadding"),
headers: <String, String>{
'Accept': 'application/json',
'x-apikey': '11111111111111111111111111111111'
},
);
print(urlEncoded);
print(response2.body);
if (response2.statusCode == 200) {
setState(() {
final decoded = json.decode(response2.body);
data = decoded['data'];
});
if ((data.attributes.lastAnalysisStats.malicious + data.attributes.lastAnalysisStats.suspicious)>= 2) {
setState(() {
virusTotal = 'Found in VirusTotal - Possibly Malicious';
});
} else
if ((data.attributes.lastAnalysisStats.suspicious + data.attributes.lastAnalysisStats.suspicious) <= 1) {
setState(() {
virusTotal = 'Found in VirusTotal - Probably not Malicious';
print((data.attributes.lastAnalysisStats.suspicious + data.attributes.lastAnalysisStats.suspicious));
});
} else {
setState(() {
virusTotal = 'Not found in VirusTotal';
});
}
Solution 1:[1]
Try to chage this code
setState(() {
final decoded = json.decode(response2.body);
data = decoded['data'];
});
to
setState(() {
final decoded = json.decode(response2.body);
data = Data.fromJson(decoded['data']);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Behzod Faiziev |
