为什么通过BOOST创造了这个JSON字符串是不是我的服务器需要一个有什么不同? [英] Why this JSON string created by BOOST is different than the one required by my server?

查看:154
本文介绍了为什么通过BOOST创造了这个JSON字符串是不是我的服务器需要一个有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用升压创建JSON字符串。我试图通过HTTP POST这个JSON字符串发送给服务器。

以下是BOOST创建的字符串

<$p$p><$c$c>{\"TokenNo\":\"XYZ123456\",\"CPUID\":\"XYZ123456\",\"CommandID\":\"05\",\"IsEncrypted\":\"0\",\"CommandString\":\"{\\\"ADD\\\":\\\"97\\\",\\\"ESTB$c$c\\\":\\\"99999999\\\",\\\"EID\\\":\\\"XY\\\",\\\"CID\\\":\\\"0154400000\\\",\\\"DATE\\\":\\\"14042015\\\",\\\"TIME\\\":\\\"1835\\\",\\\"IOMODE\\\":\\\"I\\\",\\\"REASON$c$c\\\":\\\"55\\\",\\\"LAT\\\":\\\"87\\\",\\\"LONG\\\":\\\"90\\\"}\"}

在HTTP POST成功,但答复的内容是不是我期待:

  HTTP 200
缓存控制:无缓存
编译:无缓存
内容类型:应用程序/ JSON的;字符集= UTF-8
过期:-1
服务器:Microsoft-IIS / 7.5
的X ASPNET-版本:4.0.30319
的X已启动方式:ASP.NET
日期:星期一,2015年4月20日13时06分58秒GMT
连接:关闭{CmdStatDesc:关于处理指令数据异常}

一个成功的发布了答复,将是这个样子:

  HTTP 200
//头的其余部分{CmdStatDesc:成功}

当我在我的服务器端(测试期间)检查此HTTP POST预期的JSON字符串是如下:

<$p$p><$c$c>{\"TokenNo\":\"XYZ123456\",\"CPUID\":\"XYZ123456\",\"CommandID\":\"05\",\"IsEncrypted\":\"0\",\"CommandString\":\"[{\\\"ADD\\\":\\\"97\\\",\\\"ESTB$c$c\\\":\\\"99999999\\\",\\\"EID\\\":\\\"XY\\\", \\CID \\:\\0154400000 \\,\\DATE \\:\\14042015 \\,\\TIME \\:\\1835 \\,\\IOMODE \\:\\I \\,\\原因code \\:\\55 \\,\\LAT \\:\\87 \\,\\长\\:\\90 \\}]}

预期(上图)字符串包含方括号[] 而由BOOST形成的JSON字符串没有。

为什么会出现这种情况?

为什么通过BOOST创建的JSON字符串比服务器端产生预期的JSON字符串不同?

注意 - 我已经测试了类似的方案上的Visual Studio(增强)和职位是成功的,在形成VS JSON字符串是按照服务器的要求(具有方括号)。但我这样做是在Linux上,当使用提振字符串是不同的。

下面是我的main.cpp

 命名空间ALPHA1
{
结构POST3
{
    上市:
    标准::字符串TokenNo;
    标准::字符串CPUID;
    标准::字符串CommandID;
    标准::字符串IsEncrypted;
    标准::字符串JSON_Cmnd_String;
};标准::字符串to_json(POST3常量和放大器; O)
{
    ptree中出;
        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;
        提高:: property_tree :: write_json(OSS,出,FALSE);    标准::字符串JSON;
    JSON = oss.str(); 返回oss.str();
}};
诠释的main()
{
        ALPHA1 :: POST3物镜{XYZ123456,XYZ123456,05,0,object1.dump(object1)};
    性病::法院LT&;&LT; to_json(OBJ)LT;&LT;的std :: ENDL;
}

和下面是构成JSON字符串另一个文件中的code:

 无效sqliteDB :: writeJson(性病:: ostream的和放大器; OS)const的{
    使用空间boost :: property_tree;
    ptree中PT;为(自动&安培;条目:AttendanceT_list)
{
        pt.add(ADD,entry.Atten_Addr);
        pt.add(ESTB code,entry.Atten_Establishment code);
        pt.add(EID,entry.Atten_EmployeeID);
        pt.add(CID,entry.Atten_CardID);
        pt.add(DATE,entry.Atten_PunchDate);
        pt.add(时间,entry.Atten_PunchTime);
        pt.add(IOMODE,entry.Atten_InOutMode);
        pt.add(理性code,entry.Atten_Reason code);
        pt.add(LAT,entry.Atten_Lat);
        pt.add(LONG,entry.Atten_Long);
}
    write_json(OS,PT,FALSE);
}


解决方案

迭代列表中创建一个ptree中数组,如下所示(如果我理解正确):

  ptree中pt_list;
为(自动&安培;项:AttendanceT_list){
    ptree中PT;
    pt.add(ADD,entry.Atten_Addr);
    // ...
    pt_list.push_back(性病:: make_pair(,PT));
}
ptree中PT;
pt.push_back(性病:: make_pair(CommandString中,pt_list));
// ...
write_json(OS,PT,FALSE);

又见:<一href=\"http://stackoverflow.com/questions/3751357/c-how-to-create-an-array-using-boostproperty-tree\">How使用boost :: property_tree创建一个数组?

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);
}

解决方案

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?

这篇关于为什么通过BOOST创造了这个JSON字符串是不是我的服务器需要一个有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆