如何在不使用任何第三方库的情况下在c ++中反序列化json字符串

时间:2012-04-24 04:49:27

标签: c++ json visual-c++

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

响应

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

2 个答案:

答案 0 :(得分:1)

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

#include <iostream>
#include <sstream>
#include <string>

const char* response = "\
\
{\
    \"Result\": \"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(response); //simulating an response stream
    const unsigned int BUFFERSIZE = 256;

    //temporary buffer
    char buffer[BUFFERSIZE];
    memset(buffer, 0, BUFFERSIZE * sizeof(char));

    //returnValue.first holds the variables name
    //returnValue.second holds the variables value
    std::pair<std::string, std::string> returnValue;

    //read until the opening bracket appears
    while(ss.peek() != '{')         
    {
        //ignore the { sign and go to next position
        ss.ignore();
    }

    //get response values until the closing bracket appears
    while(ss.peek() != '}')
    {
        //read until a opening variable quote sign appears
        ss.get(buffer, BUFFERSIZE, '\"'); 
        //and ignore it (go to next position in stream)
        ss.ignore();

        //read variable token excluding the closing variable quote sign
        ss.get(buffer, BUFFERSIZE, '\"');
        //and ignore it (go to next position in stream)
        ss.ignore();
        //store the variable name
        returnValue.first = buffer;

        //read until opening value quote appears(skips the : sign)
        ss.get(buffer, BUFFERSIZE, '\"');
        //and ignore it (go to next position in stream)
        ss.ignore();

        //read value token excluding the closing value quote sign
        ss.get(buffer, BUFFERSIZE, '\"');
        //and ignore it (go to next position in stream)
        ss.ignore();
        //store the variable name
        returnValue.second = buffer;

        //do something with those extracted values
        std::cout << "Read " << returnValue.first<< " = " << returnValue.second<< std::endl;
    }
}

答案 1 :(得分:1)

以下是一个小例子,说明如何将 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"
}

现在,假设您希望以键:文件的方式接收所有项目。这就是你如何做到这一点:

#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