lundi 25 octobre 2021

Create json string dynamically using a function in C++

I want to create a json string using C++ function like a simple example below:

string createJson(string one, string two, string three, string four)
{
    boost::proprty_tree::ptree pt;
    std::stringstream ss;

    pt.put("one", one);
    pt.put("two", two);
    pt.put("three", three);
    pt.put("four", four);

    return ss.str();
}

int main()
{
    string jsonstr;
    //assume one below are initialised any strings
    string one, two, three, four;
    for(i = 0; i < m; i++)
    {
        tempstr = createJson(one, two, three, four);
        jsonstr.append(tempstr);
    }
    cout << jsonstr << endl;
}

The output should be in proper json format. But somewhere I am getting improper json format when all loops run. The json needs to be sent to client application from server and should be processed with json form only. Is it the right way to do it?

Please feel free to assume json values(int, string etc).

Aucun commentaire:

Enregistrer un commentaire