Why this JSON string created by BOOST is different

2019-09-11 02:28发布

问题:

I am using boost for creating JSON string. I am trying to send this JSON string to server via http POST.

The following is the string created by BOOST:

{"TokenNo":"XYZ123456","CPUID":"XYZ123456","CommandID":"05","IsEncrypted":"0","CommandString":"{\"ADD\":\"97\",\"ESTBCODE\":\"99999999\",\"EID\":\"XY\",\"CID\":\"0154400000\",\"DATE\":\"14042015\",\"TIME\":\"1835\",\"IOMODE\":\"I\",\"REASONCODE\":\"55\",\"LAT\":\"87\",\"LONG\":\"90\"}"}

The http POST is successful but the reply is not what I am expecting:

HTTP 200
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 20 Apr 2015 13:06:58 GMT
Connection: close

{"CmdStatDesc":"Exception on processing command data"}

for a successful POSTing the reply will look something like this:

HTTP 200
//rest of the header

{"CmdStatDesc":"SUCCESS"}

when I checked (during testing) on my server side the expected json string for this http POST is as following:

{"TokenNo":"XYZ123456","CPUID":"XYZ123456","CommandID":"05","IsEncrypted":"0","CommandString":"[{\"ADD\":\"97\",\"ESTBCODE\":\"99999999\",\"EID\":\"XY\", \"CID\":\"0154400000\",\"DATE\":\"14042015\", \"TIME\":\"1835\",\"IOMODE\":\"I\",\"REASONCODE\":\"55\",\"LAT\":\"87\",\"LONG\":\"90\"}]"}

the expected(above) string contains square brackets [] whereas the JSON string formed by BOOST does not.

Why is this happening ?

Why the json string created by BOOST is different than the expected json string created at server end ?

NOTE- I have tested the similar program on visual studio (no BOOST) and the post is successful, the json string formed in VS is as per the server requirement(having the square bracket). But when I do this on linux the using boost the string is different.

following is my main.cpp

namespace ALPHA1
{
struct POST3
{
    public:
    std::string TokenNo;
    std::string CPUID;
    std::string CommandID;
    std::string IsEncrypted;
    std::string JSON_Cmnd_String;
};

std::string to_json(POST3 const& o)
{
    ptree out;
        out.add("TokenNo", o.TokenNo);
        out.add("CPUID", o.CPUID);
        out.add("CommandID", o.CommandID);
        out.add("IsEncrypted", o.IsEncrypted);
        out.add("CommandString", o.JSON_Cmnd_String);

        std::ostringstream oss;
        boost::property_tree::write_json(oss, out, false);

    std:: string json;
    json = oss.str();       

 return oss.str();
}

};
int main()
{
        ALPHA1::POST3 obj { "XYZ123456", "XYZ123456", "05", "0", object1.dump(object1)};
    std::cout <<to_json(obj) << std::endl;
}

And following is the code in another file which forms json string:

void sqliteDB::writeJson(std::ostream& os) const {
    using namespace boost::property_tree;
    ptree pt;

for (auto &entry : AttendanceT_list) 
{       
        pt.add("ADD", entry.Atten_Addr);
        pt.add("ESTBCODE", entry.Atten_EstablishmentCode);
        pt.add("EID", entry.Atten_EmployeeID);
        pt.add("CID", entry.Atten_CardID);
        pt.add("DATE", entry.Atten_PunchDate);
        pt.add("TIME", entry.Atten_PunchTime);
        pt.add("IOMODE", entry.Atten_InOutMode);
        pt.add("REASONCODE", entry.Atten_ReasonCode);
        pt.add("LAT", entry.Atten_Lat);
        pt.add("LONG", entry.Atten_Long);                   
}
    write_json(os, pt, false);
}

回答1:

Iterate the list to create an ptree array, as follows(If I understand correctly):

ptree pt_list;
for (auto &entry : AttendanceT_list) {       
    ptree pt;
    pt.add("ADD", entry.Atten_Addr);
    // ...
    pt_list.push_back(std::make_pair("", pt));
}
ptree pt;
pt.push_back(std::make_pair("CommandString", pt_list));
// ...
write_json(os, pt, false);

see also: How to create an array using boost::property_tree?