Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c++;不使用任何第三方库_C++_Json_Visual C++ - Fatal编程技术网

C++ 如何在c++;不使用任何第三方库

C++ 如何在c++;不使用任何第三方库,c++,json,visual-c++,C++,Json,Visual C++,我正在vc++中创建一个应用程序,以json格式调用webservice,而不使用json库来序列化/反序列化字符串。我正在通过手动构造来发送json字符串。有人能帮助我如何在不使用c中任何库的情况下反序列化jason字符串吗++ 回应 { "Result": "1", "gs":"0", "ga":"0", "la":"0", "lb":"0", "lc":"0", "ld":"0", "ex":"0", "gd":"0

我正在vc++中创建一个应用程序,以json格式调用webservice,而不使用json库来序列化/反序列化字符串。我正在通过手动构造来发送json字符串。有人能帮助我如何在不使用c中任何库的情况下反序列化jason字符串吗++

回应

{ 
    "Result": "1",
    "gs":"0",
    "ga":"0",
    "la":"0",
    "lb":"0",
    "lc":"0",
    "ld":"0",
    "ex":"0",
    "gd":"0"        
}

这只是使用stl解析响应字符串的一个粗略实现,但您可以将其用作进一步处理的起点。如果您可以使用任何正则表达式(例如),那么这个解析可以做得更简单,但是您也可以使用一个特定的json解析器,所以忘了这一点;)

#包括
#包括
#包括
常量字符*响应=”\
\
{\
\“结果\”:“1\”\
\“gs\”:“0\”\
\“ga\”:“0\”\
\“la\”:“0\”\
\“lb\”:“0\”\
\“lc\”:“0\”\
\“ld\”:“0\”\
\“ex\”:“0\”\
\“gd\”:\“0\”\
}";
int main(int argc,char*argv[])
{
std::stringstream ss(响应);//模拟响应流
const unsigned int BUFFERSIZE=256;
//临时缓冲区
字符缓冲区[BUFFERSIZE];
memset(buffer,0,BUFFERSIZE*sizeof(char));
//returnValue.first保存变量名称
//returnValue.second保存变量值
std::对返回值;
//阅读,直到出现左括号
而(ss.peek()!='{')
{
//忽略{号并转到下一个位置
忽略();
}
//获取响应值,直到出现右括号
而(ss.peek()!='}')
{
//读取,直到出现开头变量引号符号
get(buffer,BUFFERSIZE,“\”);
//并忽略它(转到流中的下一个位置)
忽略();
//读取变量标记,不包括结束变量引号符号
get(buffer,BUFFERSIZE,“\”);
//并忽略它(转到流中的下一个位置)
忽略();
//存储变量名
returnValue.first=缓冲区;
//读取,直到出现起始值引号(跳过:符号)
get(buffer,BUFFERSIZE,“\”);
//并忽略它(转到流中的下一个位置)
忽略();
//不包括收盘价报价符号的读取值标记
get(buffer,BUFFERSIZE,“\”);
//并忽略它(转到流中的下一个位置)
忽略();
//存储变量名
returnValue.second=缓冲区;
//对这些提取的值进行处理

std::cout下面是一个小例子,说明如何使用boost::spirit::qi实现这些目的

请注意,Boost确实是一个第三方库

假设您收到了一个JSON文件,并将其保存在JSON example.txt中,其中包含以下内容:

{ "Result":"1", "gs":"0", "ga":"0", "la":"0", "lb":"0", "lc":"0", "ld":"0", "ex":"0", "gd":"0" } { “结果”:“1”, “gs”:“0”, “ga”:“0”, “la”:“0”, “lb”:“0”, “lc”:“0”, “ld”:“0”, “ex”:“0”, “gd”:“0” } 现在,假设您希望以键:file的方式接收所有项目。您可以这样做:

#include <vector>
#include <string>
#include <fstream>
#include <map>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/std_pair.hpp>

namespace qi = boost::spirit::qi;

template<typename Iterator>
struct jsonparser : qi::grammar<Iterator, std::map<std::string, int>()>
{
    jsonparser() : jsonparser::base_type(query, "JSON-parser")
    {
        using qi::char_;
        using qi::int_;
        using qi::blank;
        using qi::eol;
        using qi::omit;

        query = omit[-(char_('{') >> eol)] >> pair % (eol | (',' >> eol)) >> '}';

        pair  = key >> -(':' >> value);

        key   = omit[*blank] >> '"' >> char_("a-zA-Z_") >> *char_("a-zA-Z_0-9") >> '"';

        value = '"' >> int_ >> '"';

    };

    qi::rule<Iterator, std::map<std::string, int>()> query;
    qi::rule<Iterator, std::pair<std::string, int>()> pair;
    qi::rule<Iterator, std::string()> key;
    qi::rule<Iterator, int()> value;

};

void main(int argc, char** argv)
{
    // Copy json-example.txt right in the std::string
    std::string jsonstr
    (
        (
            std::istreambuf_iterator<char>
            (
                *(std::auto_ptr<std::ifstream>(new std::ifstream("json-example.txt"))).get()
            )
        ),
        std::istreambuf_iterator<char>()
    );

    typedef std::string::iterator StrIterator;

    StrIterator iter_beg = jsonstr.begin();
    StrIterator iter_end = jsonstr.end();

    jsonparser<StrIterator> grammar;

    std::map<std::string,int> output;

    // Parse the given json file
    qi::parse(iter_beg, iter_end, grammatic, output);

    // Output the result
    std::for_each(output.begin(), output.end(), 
            [](const std::pair<std::string, int> &item) -> void 
            { 
                    std::cout << item.first << ":" << item.second << std::endl; 
            });
}
#包括
#包括
#包括
#包括
#包括
#包括
名称空间qi=boost::spirit::qi;
模板
结构jsonparser:qi::语法
{
jsonparser():jsonparser::基本类型(查询,“JSON解析器”)
{
使用qi::char\ux;
使用qi::int_;
使用qi::blank;
使用qi::eol;
使用qi::省略;
query=omit[-(char|('{')>>eol)]>>pair%(eol|(','>>eol))>>'};
pair=键>>-(':'>>值);
key=omit[*blank]>>'''>>char_u2;(“a-zA-Z”)>>*char_2;(“a-zA-Z_0-9”)>';
值=“””>>int_>>“”;
};
qi::规则查询;
qi:规则对;
qi::规则键;
qi:规则值;
};
void main(整型argc,字符**argv)
{
//将json-example.txt复制到std::字符串中
std::string jsonstr
(
(
std::istreambuf_迭代器
(
*(std::auto_ptr(新std::ifstream(“json example.txt”)).get()
)
),
std::istreambuf_迭代器()
);
typedef std::string::迭代器striiter;
StrIterator iter_beg=jsonstr.begin();
StrIterator iter_end=jsonstr.end();
jsonparser语法;
地图输出;
//解析给定的json文件
解析(iter\u beg,iter\u end,grammatic,output);
//输出结果
std::for_each(output.begin()、output.end(),
[](const std::pair&item)->void
{ 

std::我可以问一个显而易见的问题吗:你到底为什么不能使用一个已经存在的JSON实现?你尝试了什么?如果你不想使用库,你就必须编写代码。所以继续这样做。当你完全无法实现某个特定的东西时,在这里问问题。像往常一样,生成代码非常容易一个库可能有点过分(虽然至少不会弄乱编码),但是解析稍微复杂一些。您可以使用boost::qi将给定的数据解析为std::map
#include <vector>
#include <string>
#include <fstream>
#include <map>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/std_pair.hpp>

namespace qi = boost::spirit::qi;

template<typename Iterator>
struct jsonparser : qi::grammar<Iterator, std::map<std::string, int>()>
{
    jsonparser() : jsonparser::base_type(query, "JSON-parser")
    {
        using qi::char_;
        using qi::int_;
        using qi::blank;
        using qi::eol;
        using qi::omit;

        query = omit[-(char_('{') >> eol)] >> pair % (eol | (',' >> eol)) >> '}';

        pair  = key >> -(':' >> value);

        key   = omit[*blank] >> '"' >> char_("a-zA-Z_") >> *char_("a-zA-Z_0-9") >> '"';

        value = '"' >> int_ >> '"';

    };

    qi::rule<Iterator, std::map<std::string, int>()> query;
    qi::rule<Iterator, std::pair<std::string, int>()> pair;
    qi::rule<Iterator, std::string()> key;
    qi::rule<Iterator, int()> value;

};

void main(int argc, char** argv)
{
    // Copy json-example.txt right in the std::string
    std::string jsonstr
    (
        (
            std::istreambuf_iterator<char>
            (
                *(std::auto_ptr<std::ifstream>(new std::ifstream("json-example.txt"))).get()
            )
        ),
        std::istreambuf_iterator<char>()
    );

    typedef std::string::iterator StrIterator;

    StrIterator iter_beg = jsonstr.begin();
    StrIterator iter_end = jsonstr.end();

    jsonparser<StrIterator> grammar;

    std::map<std::string,int> output;

    // Parse the given json file
    qi::parse(iter_beg, iter_end, grammatic, output);

    // Output the result
    std::for_each(output.begin(), output.end(), 
            [](const std::pair<std::string, int> &item) -> void 
            { 
                    std::cout << item.first << ":" << item.second << std::endl; 
            });
}
Result:1 gs:0 ga:0 la:0 lb:0 lc:0 ld:0 ex:0 gd:0