Json和Xml相比有个最大的优势,基于字符串。xml必须与文件相关,而json只是字符串(当然也提供了与文件相关的操作)。
Let's say it from my code:
#include <json/json.h> //解压后找到目录 vs71,用vs打开然后生成解决方案,本程序直接在jsontest.cpp中改写的
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const string g_file = "json.c"; //原本以为vs打开该文件后会对json格式字符串做一个format,结果和记事本打开一样,不如xml条理清晰
// 此处两个结构体并没有用到,他们的目的只是让我们看清楚json字符串的格式
struct Address
{
string name; //街道名
int number; //街道号
};
struct Student
{
int no; //学好
string name; //名字
Address addr; //家庭地址
};
void Write()
{
Json::Value root; //根(如果树的根一样)
int no[] = { 2008, 2010, 2013 };
string name[] = { "sumos", "fly away", "sun"};
string name2[] = { "西湖路", "东湖路", "中南海" };
int number[] = { 101, 202, 303 };
for(in