Thread: C++ convert JSON to XML

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    1

    C++ convert JSON to XML

    I am looking for a way to convert JSON to XML In C++.
    For this we have evaluated PICOJSON and Pugixml.

    I need some guidance and approach during initial coding on how to do this.

    I am thinking of first Parsing the JSOn into and Object and then Iterated the Object recursively and construct XML using Pugixml

    Code:
    #include <iostream>
    #include "include/picojson.h";
    #include "include/pugixml.hpp";
    using namespace std;
    int main() {
       
        const char* json =
            "{"menu": {"
            ""id": "f","
            ""popup": {"
            "  "menuitem": ["
            "    {"v": "0"},"
            "    {"v": "1"},"
            "    {"v": "2"}"
            "   ]"
            "  }"
            "}"
            "}";
        //const char* json = "{"a":1}";
        picojson::value v;
        std::string err;
        const char* json_end = picojson::parse(v, json, json + strlen(json), &err);
        std::string jsonres = v.serialize();
       
        std::cout << v;
        //How should I iterate on this "v" or "jsonres"
    
    
        return 0;
    }
    How can I iterate the Parsed JSON Object, could someone guide me with initial code/sudo code

  2. #2
    Registered User
    Join Date
    May 2021
    Posts
    1

    Unhappy Two approaches

    I mixed visual studio versions and Now I had to remove everything because it bugged everything... Any ways once I got a working setup I would give you a better answer but here is my take any ways and I hope it helps.


    Firstly always write the initial result to debug so you have a clue what is going on with the output.
    Secondly, XML is a true Array, whilst Json is just more of an object that is like an array.


    I'd say you have two approaches either you use this library to do it for you "http://www.boost.org/doc/libs/1_55_0/doc/html/property_tree.html" or manually add + i iteration you may have used
    inside of loop functions before "https://www.example-code.com/cpp/json_array_iterate.asp"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to read json file
    By bintech21 in forum Projects and Job Recruitment
    Replies: 8
    Last Post: 02-20-2020, 12:06 PM
  2. JSON Advise for C++
    By JessicaS in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2016, 05:39 AM
  3. Head First C Google Map associated with JSON file
    By Vesnog in forum C Programming
    Replies: 4
    Last Post: 04-05-2014, 05:57 PM
  4. Replies: 17
    Last Post: 07-26-2012, 09:32 PM
  5. how can i convert DOC to PDF?
    By PedroTuga in forum Tech Board
    Replies: 6
    Last Post: 05-17-2004, 01:10 PM

Tags for this Thread