用rapidjson解析json数据

  1. rapid安装说明:https://blog.csdn.net/weixin_44517656/article/details/109327617
  2. rapidjson教程:http://rapidjson.org/zh-cn/md_doc_tutorial_8zh-cn.html
  3. 实例
#include "rapidjson/include/rapidjson/document.h"
#include <iostream>


using namespace rapidjson;
using namespace std;

int main()
{
    const char* json1 = "{\"project\":\"rapidjson\",\"stars\":10, \"a\":[1,2,3,4]}";
    Document document1;
    document1.Parse(json1);
    cout << document1["project"].GetString() << endl;
    for(int i = 0;i < 4; i++) {
      cout << document1["a"][i].GetInt() << endl;
    }

    const char* json = "[{\"a\":0.623,\"b\":-1},{\"a\":0.523,\"b\":-1}]";
    Document document;
    document.Parse(json);
    if(document.IsArray()){
      for(int i = 0; i < document.Size(); i++) {
        if (document[i].IsObject()){
          cout << document[i]["a"].GetDouble() << endl;
        }
      }
    }
}
posted @ 2022-04-13 10:44  baishengguan  阅读(1655)  评论(0编辑  收藏  举报