1
我使用Json-cpp解析我的配置文件,我得到som奇怪的行爲與asCString() ..任何人都可以解釋爲什麼輸出爲2是空的?Json cpp asCstring()返回空cstring當存儲在變量中
#include <iostream>
#include <fstream>
#define JSON_IS_AMALGAMATION
#include "json/json.h"
using std::cout;
using std::endl;
int main(int argc, char** argv) {
Json::Value root;
Json::Reader reader;
std::ifstream config("dev.json", std::ifstream::binary);
if (!reader.parse(config, root, false)) {
cout << "Could not parse json" << endl;
return 1;
}
std::string str = root["redis"].get("host", "localhost").asString();
const char* cstr = root["redis"].get("host", "localhost").asCString();
cout << "1:" << str << endl;
cout << "2:" << cstr << endl;
cout << "3:" << std::string(root["redis"].get("host", "localhost").asCString()) << endl;
config.close();
return 0;
}
輸出:
c++ -o test test.cpp jsoncpp.cpp
1:127.0.0.2
2:
3:127.0.0.2
我的JSON數據:
{ "redis": { "host": "127.0.0.2", "port": 6379 } }
是的,get()確實會返回一個值。謝謝! – Jesper 2012-07-05 12:38:49