1

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.

4
  • Does this answer your question? Flutter Dart Http XML rest convert to Json Commented 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. Commented Apr 10, 2023 at 13:13
  • Actually I have to convert response to a dart model, that's what I meant Commented Apr 10, 2023 at 13:32
  • 1
    Does this answer your question? How to import xml file to dart object Commented Apr 11, 2023 at 0:06

1 Answer 1

3

you can convert an XML response to JSON by using the xml2json package. Here are the steps to do it:

  1. Add the xml2json package to your pubspec.yaml file and run flutter pub get to install it.

    dependencies:
    xml2json: ^4.2.0
    
  2. Import 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);
    
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.