2017-07-21 96 views
0

如何读取*.json文件并将输出放在std::string上?如何使用rapidjson读取json文件并输出到std :: string?

我有这个样本,但我总是得到nullstd::string

#include <rapidjson/document.h> 
#include <rapidjson/istreamwrapper.h> 
#include "rapidjson/writer.h" 
#include "rapidjson/stringbuffer.h" 
#include <rapidjson/ostreamwrapper.h> 
#include <fstream> 
#include <iostream> 

using namespace rapidjson; 
using namespace std; 

void main() 
{ 
    ifstream ifs("input.json"); 
    IStreamWrapper isw(ifs); 
    Document d; 
    d.ParseStream(isw); 

    StringBuffer buffer; 
    Writer<StringBuffer> writer(buffer); 
    d.Accept(writer); 

    std::string jsonStr(buffer.GetString()); 
    if(jsonStr == "null") 
     std::cout << "is null..." << std::endl; //<--always here! 
    else 
    { 
     std::cout << jsonStr.c_str() << std::endl; 

     d["ip"] = "123456789"; 

     ofstream ofs("output.json"); 
     OStreamWrapper osw(ofs); 
     Writer<OStreamWrapper> writer2(osw); 
     d.Accept(writer2); 
    } 
} 

这是我的JSON文件:

{ 
    "ip" : "192.168.0.100", 
    "angle x": 20, 
    "angle y": 0, 
    "angle z": 0 
} 
+1

你到底想实现什么呢?如果你想将整个文件读入'string',那么你不需要一个quickjson。 – Heavy

+0

我想从文件中读取,更改json上的一些字段,写回到另一个文件。 – waas1919

+0

@ waas1919:你的文件'input.json'包含什么?在这里发布最小有效的JSON。您是否使用[HasParseError()](http://rapidjson.org/classrapidjson_1_1_generic_document.html#a7607bb42b51547e44bfd4cab35d8f20e)和[GetParseError()](http://rapidjson.org/classrapidjson_1_1_generic_document.html#ab94c280c079a6837a24951cb4d8f337b)验证您的解析是否成功? ? – Azeem

回答

2

在转换为std::string之前,您需要检查所有错误。确保该文件已打开进行读取和写入,并且解析成功,即JSON有效。 GetParseError()GetErrorOffset()是验证解析的函数。

我已经使用过您的示例并对其进行了增强。希望你不会介意。 :-)

这里有一个工作example

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <rapidjson/document.h> 
#include <rapidjson/istreamwrapper.h> 
#include <rapidjson/writer.h> 
#include <rapidjson/stringbuffer.h> 
#include <rapidjson/ostreamwrapper.h> 

int main() 
{ 
    using namespace rapidjson; 

    std::ifstream ifs { R"(C:\Test\Test.json)" }; 
    if (!ifs.is_open()) 
    { 
     std::cerr << "Could not open file for reading!\n"; 
     return EXIT_FAILURE; 
    } 

    IStreamWrapper isw { ifs }; 

    Document doc {}; 
    doc.ParseStream(isw); 

    StringBuffer buffer {}; 
    Writer<StringBuffer> writer { buffer }; 
    doc.Accept(writer); 

    if (doc.HasParseError()) 
    { 
     std::cout << "Error : " << doc.GetParseError() << '\n' 
        << "Offset : " << doc.GetErrorOffset() << '\n'; 
     return EXIT_FAILURE; 
    } 

    const std::string jsonStr { buffer.GetString() }; 

    std::cout << jsonStr << '\n'; 

    doc[ "ip" ] = "127.0.0.1"; 

    std::ofstream ofs { R"(C:\Test\NewTest.json)" }; 
    if (!ofs.is_open()) 
    { 
     std::cerr << "Could not open file for writing!\n"; 
     return EXIT_FAILURE; 
    } 

    OStreamWrapper osw { ofs }; 
    Writer<OStreamWrapper> writer2 { osw }; 
    doc.Accept(writer2); 

    return EXIT_SUCCESS; 
} 
0

我没有足够的代表处发表评论,所以我在回答中写的。

如果您打算使用其他库,nlohmann json是一个整齐的,只有标题的库。

如在网站上给出的例子,从文件中读取并把它转换成字符串可以是简单的

// read a JSON file 
std::ifstream i("file.json"); 
json j; 
i >> j; 

std::string s = j.dump(); 

对不起,我没用过rapidjson自己。

相关问题