I am receiving an API response in XML format in my Flutter app and I need to convert it to JSON to process the data. I'm already using xml for sending data to api's in xml format.
-
Does this answer your question? Flutter Dart Http XML rest convert to JsonMonasha– Monasha2023-04-10 09:46:21 +00:00Commented Apr 10, 2023 at 9:46
-
"I need to convert it to JSON to process the data" I'm not a Dart developer but that doesn't really make any sense: typically in a programming language you process data that's been serialized to a text format like XML or JSON by parsing it into data structures in your programming language, not by converting it to a different text format.Jared Smith– Jared Smith2023-04-10 13:13:59 +00:00Commented Apr 10, 2023 at 13:13
-
Actually I have to convert response to a dart model, that's what I meantJawad Abbasi– Jawad Abbasi2023-04-10 13:32:43 +00:00Commented Apr 10, 2023 at 13:32
-
1Does this answer your question? How to import xml file to dart objectJared Smith– Jared Smith2023-04-11 00:06:55 +00:00Commented Apr 11, 2023 at 0:06
Add a comment
|
1 Answer
you can convert an XML response to JSON by using the xml2json package. Here are the steps to do it:
Add the xml2json package to your pubspec.yaml file and run flutter pub get to install it.
dependencies: xml2json: ^4.2.0Import the xml2json package in your Dart file.
final xmlString = "<person><name>John</name><age>30</age></person>"; final xml2json = Xml2Json(); xml2json.parse(xmlString); final jsonString = xml2json.toParker(); final jsonObject = json.decode(jsonString);