0

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.

Senthur Kumaran
  • 1,135
  • 1
  • 7
  • 18

1 Answers1

0

I am already using the xml2json package.

No, you don't. There is no such thing in your code. You are just calling meaningless functions that act as if XMl data were json. It's not and that is why you get errors.

I suggest you actually read the example on the package you are using and then write your code accordingly. A package is not magic that you reference and then it works. You have to call their functions.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • sorry, friend. I forgot to mention that I am using `import 'dart:convert';` to convert the XML file. Without any package, we can convert XML to JSON with `jsonDecode()` in dart. And that is what I am trying to do here. With the xml2json package, I already got the output. I want to know whether this code work or not(large file). This method works for small XML code (I think so). [You can see this question.](https://stackoverflow.com/questions/50067455/string-xml-file-in-flutter). I have edited the question as well. – Senthur Kumaran Oct 12 '21 at 05:18
  • "we can convert XML to JSON with jsonDecode() in dart". No you cannot. Why would there even be an xml2json package if that was true? The linked question says no such thing either. Nor does the documentation. – nvoigt Oct 12 '21 at 05:47
  • Thanks, buddy. For clearing this. Seems, I misunderstood the whole thing. If I am right, The `jsonDecode()` is used to store the values as an object with value and `jsonEncode()` as String in the list. If this is wrong, please post an answer that explains what is `jsonDecode()` and `jsonEncode()`. – Senthur Kumaran Oct 16 '21 at 06:29