【问题标题】:Writing lines of file into JSON using C++ and jsoncpp使用 C++ 和 jsoncpp 将文件行写入 JSON
【发布时间】:2013-07-14 21:28:49
【问题描述】:

我有一个文本文件,我试图在我的 c++ 应用程序中使用 jsoncpp 将其转换为 JSON 对象。

文件内容的格式如下:

system type : Atheros AR7241 rev 1
machine     : Ubiquiti UniFi
processor   : 0
cpu model   : MIPS 24Kc V7.4
BogoMIPS    : 259.27

这似乎很容易开始。我需要键来匹配第一列和第二列的值,如下所示:

{ "meh" : [{ "system type" : "Atheros AR7241 rev 1", "machine" : "Ubiquiti UniFi" ...

我可以将它的整个文件写入一个 json 对象。但这就是我所能得到的……

Json::Value root;
string line;

ifstream myfile( "/proc/cpuinfo" );
if (myfile)
{
    while (getline( myfile, line ))
    {
        root["test"] = line;
        cout << root;
    }
    myfile.close();
}

这很接近,但显然给了我这样的 json:

{
    "test" : "system type   : Atheros AR7241 rev 1"
}

我是 C++ 新手,我不知道 如何在冒号处拆分行并使用前半部分作为键而不是“测试”。有人可以建议一种方法吗这个?

【问题讨论】:

    标签: c++ json jsoncpp


    【解决方案1】:

    我建议使用"string::find()""string::substr()" 的组合。

    或者一些正则表达式的东西,但我认为这需要通过标准库。

    例子:

    std::string::size_type n;
    std::string key;
    std::string value;
    
    n = line.find(':');
    
    key = line.substr( 0, n );
    value = line.substr( n+1 );
    

    然后可能需要从白色字符中去除键和值。我不会涵盖它,因为关于如何做到这一点的问题和答案很少。例如:

    1. Similar function in C++ to Python's strip()?
    2. What's the best way to trim std::string?

    编辑:

    完整示例代码split.cpp:

    /// TRIMMING STUFF
    // taken from https://stackoverflow.com/a/217605/1133179
    #include <algorithm> 
    #include <functional> 
    #include <cctype>
    #include <locale>
    
    // trim from start
    static inline std::string &ltrim(std::string &s) {
            s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
            return s;
    }
    
    // trim from end
    static inline std::string &rtrim(std::string &s) {
            s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
            return s;
    }
    
    // trim from both ends
    static inline std::string &trim(std::string &s) { return ltrim(rtrim(s)); }
    /// ~~~ TRIMMING STUFF ~~~
    
    
    #include <string>
    #include <iostream>
    
    int main(){
    std::string::size_type n;
    std::string key;
    std::string value;
    
    std::string line = "system type : Atheros AR7241 rev 1";
    
    n = line.find(':');
    
    key = line.substr( 0, n );
    value = line.substr( n+1 );
    std::cout << "line: `" << line << "`.\n";
    std::cout << "key: `" << key << "`.\n";
    std::cout << "value: `" << value << "`.\n";
    
    
    ///  With trimming
    
    std::cout << "{\n "json" : [{ \"" << trim(key) << "\" : \"" << trim(value) << "\" }]\n}\n";
    }
    

    执行:

    luk32:~/projects/tests$ g++ ./split.cpp
    luk32:~/projects/tests$ ./a.out 
    line: `system type : Atheros AR7241 rev 1`.
    key: `system type `.
    value: ` Atheros AR7241 rev 1`.
    {
     "json" : [{ "system type" : "Atheros AR7241 rev 1" }]
    }
    

    我觉得还可以。

    【讨论】:

    • 谢谢@luk32,是的,我们必须让程序停止运行——正则表达式现在已经出来了。真的不知道从哪里开始,现在就去看看。
    • @simonmorley 我把它放到一个 cpp 文件中并执行。有一些小问题。我编辑了所有内容并发布了整个代码。我认为结果是令人满意的,或者至少从这里拿起它是微不足道的。
    • 那行得通。 (必须将 json 包装为 \"json\")。必须弄清楚如何将整个文件包装为单个对象。但似乎可以这样做..
    猜你喜欢
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    • 1970-01-01
    相关资源
    最近更新 更多