json.txt中的内容:
{
"name":"张三",
"age":18,
"身高":1.81,
"生日":"2002-01-02",
"学校":"北大青鸟",
"专业":["厨师","挖掘机"],
"单身":true,
"地址":null
}
#include <iostream>
#include "../jsonlib/json.h"
#include <string>
#include <map>
#define BUFFER_SIZE 4096
int main()
{
FILE* pFile = fopen("./json.txt", "r");
if (pFile) {
std::string buffer;
buffer.resize(BUFFER_SIZE);
memset((char*)buffer.c_str(), 0, BUFFER_SIZE);
size_t len = fread((char*)buffer.c_str(), 1, BUFFER_SIZE, pFile);
buffer.resize(len);
fclose(pFile);
Json::Value root;
Json::Reader reader;
if (reader.parse(buffer, root))
{
Json::Value::Members members = root.getMemberNames();
for (unsigned i = 0; i < members.size(); i++)
{
switch (root[members[i]].type()) {
case Json::intValue:
std::cout << members[i] << "=i=>" << root[members[i]].asInt() << std::endl;
break;
case Json::stringValue:
std::cout << members[i] << "=s=>" << root[members[i]].asString() << std::endl;
break;
default:
std::cout << members[i] << "==>" << root[members[i]].toStyledString() << std::endl;
break;
}
}
std::string data = root.toStyledString();
std::cout << data << std::endl;
}
root.clear();
root["bool"] = false;
root["array"] = Json::Value(Json::arrayValue);
root["array"].append("hello");
root["array"].append("world");
std::cout << root.toStyledString();
}
}