加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

jsoncpp构造json字符串和json数组

发布时间:2020-12-16 19:02:19 所属栏目:百科 来源:网络整理
导读:jsoncpp构造json字符串和json数组 参考文章:Jsoncpp的简单使用 下载json文件夹放在c++项目的include目录下,在CMakeLists中include进去,然后就可以在代码中加入#include “json/json.h”使用啦。下载地址:https://github.com/open-source-parsers/jsoncpp

jsoncpp构造json字符串和json数组

参考文章:Jsoncpp的简单使用
下载json文件夹放在c++项目的include目录下,在CMakeLists中include进去,然后就可以在代码中加入#include “json/json.h”使用啦。下载地址:https://github.com/open-source-parsers/jsoncpp/tree/master/include。

  • jsoncpp构造json字符串
Json::Value root;
    Json::FastWriter writer;
    string name = "abcd";
    root["name"] = name;
    root["number"] = "2010014357";
    root["address"] = "xxxx";
    root["age"] = 100;
    string data= writer.write(root); //need #include <fstream>
    cout<<"data:n"<<data<<endl; 
  • jsoncpp构造json数组
#include "json/json.h"
    #include <fstream>
    #include <iostream>
    using namespace std;
    int main()
    {
        Json::Value root;
        Json::FastWriter writer;
        Json::Value person;
        person["name"] = "hello world1";
        person["age"] = 100;
        root.append(person);
        person["name"] = "hello world2";
        person["age"] = 200;
        root.append(person);
        string data= writer.write(root);
        cout<<data<<endl;   
        cout<<root.toStyledString()<<endl;
        return 0;
    }

输出为:
前者就是一般的输出,root.toStyledString()比较规整。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读