I am converting a XML file to JSON file using jsonDecode(jsonEncode(file))
. The problem is when I try to Fetch data from the JSON file with an index number. It prints only one letter. I have inserted the code below. See if there is any problem with my code. If I try to fetch data with string it shows error.
My file is large. So, I am only inserting a small portion of the output.
I have already tried the xml2json package and I got the output.
But in dart without any package we can convert the XML file to JSON with import 'dart:convert';
. With the documentation, I came to understand it only works for small files. If it also works for large files, I want to know the problem in this code and the solution.
And that is what I tried in the following code.
This is my code,
Future<String> decodexml() async {
final file =
await rootBundle.loadString('assets/xml_file/belovedskincare.xml');
final jsonData = jsonDecode(jsonEncode(file));
debugPrint('$jsonData');
return jsonData;
}
When I print $jsonData
this is the output,
<rss version="2.0"
I/flutter (24058): xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
I/flutter (24058): xmlns:content="http://purl.org/rss/1.0/modules/content/"
I/flutter (24058): xmlns:wfw="http://wellformedweb.org/CommentAPI/"
I/flutter (24058): xmlns:dc="http://purl.org/dc/elements/1.1/"
I/flutter (24058): xmlns:wp="http://wordpress.org/export/1.2/"
I/flutter (24058): >
I/flutter (24058):
I/flutter (24058): <channel>
I/flutter (24058): <title>Beloved Skincare</title>
I/flutter (24058): <link>https://belovedskincare.com.my</link>
I/flutter (24058): <description>Food for your Skin</description>
I/flutter (24058): <pubDate>Sat, 02 Oct 2021 09:17:04 +0000</pubDate>
I/flutter (24058): <language>en-US</language>
I/flutter (24058): <wp:wxr_version>1.2</wp:wxr_version>
I/flutter (24058): <wp:base_site_url>https://belovedskincare.com.my</wp:base_site_url>
I/flutter (24058): <wp:base_blog_url>https://belovedskincare.com.my</wp:base_blog_url>
When I try to print some data from jsonData
, this is the output I get,
code:
var json = jsonData['rss'];
debugPrint('$json');
output:
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
code:
var json = jsonData[0];
debugPrint('$json');
output:
I/flutter (24058): <
code:
var json = jsonData[1];
debugPrint('$json');
output:
I/flutter (24058): r
To my understanding, It seems each letter from the json file saved separately in the jsonData
variable.