欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RapidJSON

程序员文章站 2024-02-04 08:26:58
...

文章目录

JSON

{
  "name": "yanggle",
  "gender": "boy",
  "hobby": [
    "足球",
    "篮球",
    "电影"
  ],
  "socre": {
    "数学": 93.5,
    "英语": 98,
    "语文": 98.5
  },
  "lover": {
    "name": "beibei",
    "gender": "girl",
    "hobby": [
      "画画",
      "跳舞",
      "唱歌"
    ],
    "score": {
      "数学": 76.5,
      "英语": 97,
      "语文": 88
    }
  }
}

#include<map>
#include<string>
#include<vector>
#include<fstream>
#include<iostream>

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"

using namespace std;

int main () {

  string name = "xiaoming";
  string gender = "boy";
  vector<string> hobby = {"足球","篮球","电影"};
  map<string, double> score = {{"数学",91.5},{"语文", 95.5},{"英语", 96}};

  string lover_name = "xiaohong";
  string love_gender = "girl";
  vector<string> lover_hobby = {"画画","跳舞","唱歌"};
  map<string, double> lover_score = {{"数学", 78.5},{"语文", 89},{"英语", 90}};

  rapidjson::StringBuffer s;
  rapidjson::Writer<rapidjson::StringBuffer> writer(s);
  writer.StartObject();

  writer.Key("name");
  writer.String(name.c_str());
  writer.Key("gender");
  writer.String(gender.c_str());

  writer.Key("hobby");
  writer.StartArray();
  for(auto &item : hobby){
    writer.String(item.c_str());
  }
  writer.EndArray();

  writer.Key("socre");
  writer.StartObject();
  for(auto &item : score){
    writer.Key((item.first).c_str());
    writer.Double(item.second);
  }
  writer.EndObject();

  writer.Key("lover");
  writer.StartObject();

  writer.Key("name");
  writer.String(lover_name.c_str());
  writer.Key("gender");
  writer.String(love_gender.c_str());

  writer.Key("hobby");
  writer.StartArray();
  for(auto &item : lover_hobby){
    writer.String((item.c_str()));
  }
  writer.EndArray();

  writer.Key("score");
  writer.StartObject();
  for(auto &item : lover_score){
    writer.Key((item.first).c_str());
    writer.Double(item.second);
  }
  writer.EndObject();

  writer.EndObject();

  writer.EndObject();

  //将生成的json数据写入json1.txt文件中
  std::string outFileName = "json1.txt";
  std::ofstream outfile(outFileName,std::ios::trunc);
  outfile << s.GetString();
  outfile.close();

  std::cout << s.GetString() << std::endl;
}

#include<string>
#include<fstream>
#include<iostream>
#include"document.h"
using namespace std;

int main () {

  std::ifstream t("./document.json");
  std::string str((std::istreambuf_iterator<char>(t)),
                  std::istreambuf_iterator<char>());

  rapidjson::Document document;
  document.Parse(str.c_str());

  rapidjson::Value::ConstMemberIterator iter = document.FindMember("name");
  if(iter != document.MemberEnd()){
    cout << "name : " << iter->value.GetString() << endl;
  }

  iter = document.FindMember("gender");
  if(iter != document.MemberEnd()){
    cout << "gender : " << iter->value.GetString() << endl;
  }

  if(document.HasMember("hobby")){
    cout << "hobby : " << endl;
    const rapidjson::Value& childValue = document["hobby"];
    for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
      cout << "    " << childValue[i].GetString() << endl;
    }
  }

  if(document.HasMember("score")){
    cout << "score : " << endl;
    const rapidjson::Value& childIter = document["score"];
    for(rapidjson::Value::ConstMemberIterator it = childIter.MemberBegin(); it != childIter.MemberEnd(); ++it){
      cout << "    " << it->name.GetString() << " : " << it->value.GetDouble() << endl;
    }
  }

  if(document.HasMember("lover")){
    cout << "lover : " << endl;
    const rapidjson::Value& chileValue = document["lover"];
    rapidjson::Value::ConstMemberIterator chileIter = chileValue.FindMember("name");
    if(chileIter != chileValue.MemberEnd()){
      cout << "    " << "name : " << chileIter->value.GetString() << endl;
    }

    chileIter = chileValue.FindMember("gender");
    if(chileIter != chileValue.MemberEnd()){
      cout << "    " << "gender : " << chileIter->value.GetString() << endl;
    }

    if(chileValue.HasMember("hobby")){
      cout << "    " << "hobby : " << endl;
      const rapidjson::Value& chile2Value = chileValue["hobby"];
      for(rapidjson::SizeType i = 0; i < chile2Value.Size(); ++i){
        cout << "        " << chile2Value[i].GetString() << endl;
      }
    }

    if(chileValue.HasMember("score")){
      cout << "    " << "score : " << endl;
      const rapidjson::Value& child2Iter = chileValue["score"];
      for(rapidjson::Value::ConstMemberIterator it = child2Iter.MemberBegin(); it != child2Iter.MemberEnd(); ++it){
        cout << "        " << it->name.GetString() << " : " << it->value.GetDouble() << endl;
      }
    }
  }
}
相关标签: C++