• rapidjson的使用


    1、包含头文件

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

    using namespace rapidjson;


    2、读json字符串内容
    string str = "{"name":"qfl","age":20,"letter":["a","b","c"],"location":{"province":"fujian","city":"xiamen","number":16},"book":[{"name":"book1","isbn":"123"},{"name":"book2","isbn":"456"}],"healthy":true}";
    rapidjson::Document doc;
    doc.Parse<0>(str.c_str());
    if(doc.HasParseError())
    {
      rapidjson::ParseErrorCode code = doc.GetParseError();
      return;
    }
    else
    {
      if(doc.HasMember("name"))
        string name = doc["name"].GetString();
      if(doc.HasMember("age"))
        int age = doc["age"].getInt();

      if(doc.HasMember("letter"))
      {
        rapidjson::Value& letter = doc["letter"];
        if(letter.IsArray() && !letter.Empty())
        {
          for(rapidjson::SizeType i=0;i<letter.Size();i++)
          {
            string strValue = letter[i].GetString();
          }
        }
      }

      if(doc.HasMember("location"))
      {
        rapidjson::Value& location = doc["location"];
        if(location.IsObject())
        {
          if(location.HasMember("province"))
            string strProvince = location["province"].GetString();
          if(location.HasMember("city"))
            string strCity = location["city"].GetString();
          if(location.HasMember("number"))
            int number = location["number"].GetInt();
        }
      }

      if(doc.HanMember("book"))
      {
        rapidjson::Value& book = doc["book"];
        if(book.IsArray() && !book.Empty())
        {
          for(rapidjson::SizeType i=0;i<book.Size();i++)
          {
            rapidjson::value& Temp = book[i];

            if(Temp.IsObject())
            {
              if(Temp.HasMember("name"))
                string strName = Temp["name"].GetString();
              if(Temp.HasMember("isbn"))
                int nValue = Temp["isbn"].getInt();
            }
          }
        }
      }

      if(doc.HasMember("healthy")
      {
        if(doc["healthy"}.GetBool())
        {
          
        }
      }
    }
    3、生成json字符串
    string str = "{"name":"qfl","age":20,"letter":["a","b","c"],"location":{"province":"fujian","city":"xiamen","number":16},"book":[{"name":"book1","isbn":"123"},{"name":"book2","isbn":"456"}],"healthy":true}";

    rapidjson::Document doc;
    doc.SetObject();
    rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();

    rapidjson::Value name("qfl",allocator);
    doc.AddMember("name",name,allocator);
    doc.AddMember("age",20,allocator);
    rapidjson::Value letter(rapidjson::kArrayType);

    rapidjson::Value letter_1("a",allocator);
    rapidjson::Value letter_2("b",allocator);
    rapidjson::Value letter_3("c",allocator);
    letter.PushBack(letter_1,allocator);
    letter.PuhBack(letter_2,allocator);
    letter.PushBack(letter_3,allocator);
    doc.AddMember("letter",letter,allocator);

    rapidjson::Value location(rapidjson::kObjectType);
    rapidjson::Value province("fujian",allocator);
    rapidjson::Value city("xiamen",allocator);

    location.AddMember("province",province,allocator);
    location.AddMember("city",city,allocator);
    location.AddMember("number",16,allocator);

    rapidjson::Value book(rapidjson::kArrayType);
    rapidjson::Value object_1(rapidjson::kObjectType);
    rapidjson::Value bname1("book1",allocator);
    rapidjson::Value bisbn1("123",allocator);

    object_1.AddMember("name",bname1,allocator);
    object_1.AddMember("isbn",bisbn1,allocator);

    book.PushBack(object_1,allocator);

    rapidjson::Value object_2(rapidjson::kObjectType);
    rapidjson::Value bname2("book2",allocator);
    rapidjson::Value bisbn2("456",allocator);
    object_2.AddMember("name",bname2,allocator);
    object_2.AddMember("isbn",bisbn2,allocator);

    book.PushBack(object_2,allocator);
    book.AddMember("book",book,allocator);
    doc.AddMember("healthy",true,allocator);
    rapidjson::StringBuffer buffer; 
    rapidjson::Writer<
    rapidjson::StringBuffer> writer(buffer);
    doc.Accept(writer);
    std::string json = std::string(buffer.GetString());

  • 相关阅读:
    C#程序调用cmd执行命令(转)
    命名管道跨进程通信实例2(转)
    C#异步编程的实现方式——ThreadPool线程池
    命名管道跨进程通信实例1(转)
    No_16_0324 Java基础学习第二十三天
    mac osx加入全局启动terminal快捷键
    UVa 164
    Android OpenGL加入光照和材料属性
    51系列小型操作系统精髓 简单实现
    ubuntu下安装tomcat
  • 原文地址:https://www.cnblogs.com/zhangnianyong/p/8432062.html
Copyright © 2020-2023  润新知