为什么这个由 BOOST 创建的 JSON 字符串与我的服务器要求的不同?
Why this JSON string created by BOOST is different than the one required by my server?
我正在使用 boost 创建 JSON 字符串。我正在尝试通过 http POST.
将此 JSON 字符串发送到服务器
以下是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\"}"}
http POST 成功,但回复不是我所期待的:
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"}
如果 POST 成功,回复将如下所示:
HTTP 200
//rest of the header
{"CmdStatDesc":"SUCCESS"}
当我在服务器端检查(测试期间)时,此 http POST 的预期 json 字符串如下:
{"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\"}]"}
预期的(以上)字符串包含 方括号 [] 而 BOOST 形成的 JSON 字符串不包含。
为什么会这样?
为什么 BOOST 创建的 json 字符串与服务器端创建的预期 json 字符串不同?
注意-我已经在 visual studio 上测试了类似的程序(无 BOOST)并且 post 成功,在 VS 中形成的 json 字符串是根据服务器要求(有方括号)。但是当我在 linux 上执行此操作时,使用 boost 字符串是不同的。
以下是我的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;
}
下面是另一个文件中形成 json 字符串的代码:
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);
}
迭代列表创建一个ptree数组,如下(如果我没理解错的话):
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);
另请参阅:How to create an array using boost::property_tree?
我正在使用 boost 创建 JSON 字符串。我正在尝试通过 http POST.
将此 JSON 字符串发送到服务器以下是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\"}"}
http POST 成功,但回复不是我所期待的:
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"}
如果 POST 成功,回复将如下所示:
HTTP 200
//rest of the header
{"CmdStatDesc":"SUCCESS"}
当我在服务器端检查(测试期间)时,此 http POST 的预期 json 字符串如下:
{"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\"}]"}
预期的(以上)字符串包含 方括号 [] 而 BOOST 形成的 JSON 字符串不包含。
为什么会这样?
为什么 BOOST 创建的 json 字符串与服务器端创建的预期 json 字符串不同?
注意-我已经在 visual studio 上测试了类似的程序(无 BOOST)并且 post 成功,在 VS 中形成的 json 字符串是根据服务器要求(有方括号)。但是当我在 linux 上执行此操作时,使用 boost 字符串是不同的。
以下是我的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;
}
下面是另一个文件中形成 json 字符串的代码:
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);
}
迭代列表创建一个ptree数组,如下(如果我没理解错的话):
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);
另请参阅:How to create an array using boost::property_tree?