1

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);
}
K.K
  • 401
  • 5
  • 22
  • Where is code, that forms json? – ForEveR Apr 20 '15 at 13:52
  • I will update in a min – K.K Apr 20 '15 at 13:53
  • 1
    I'm going to go out on a limb and guess that you get a `CommandString` describing a JSON object rather than one describing a JSON array because `object1` does not describe an array. – Wintermute Apr 20 '15 at 14:24
  • 1
    You're gonna have to attack your problems with some more attention. Almost exactly a month ago [I told you](http://stackoverflow.com/questions/29278128/how-get-name-value-pair-when-creating-json-string-from-using-json-boost-serializ#comment46755017_29278128): I'd suggest google, boost property tree json array leads me to Creating JSON arrays in Boost using Property Trees and the Boost docs saying _"JSON arrays are mapped to nodes. Each element is a child node with an empty name."_. JSON is known as a very simple format. Get the ~5 datatypes it supports into your system. – sehe Apr 20 '15 at 20:11
  • @Wintermute yes, now I see what I am doing wrong. Thank you both of you. – K.K Apr 21 '15 at 04:55
  • @sehe Yes. Now all this has started making sense to me. I see what you mean. Thank you again. – K.K Apr 21 '15 at 04:56

1 Answers1

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?

Community
  • 1
  • 1
douyw
  • 4,034
  • 1
  • 30
  • 28