1. 程式人生 > >jsoncpp構造json字符串和json數組

jsoncpp構造json字符串和json數組

ott 文件 xxx cpp ostream ren led mas tle

參考文章: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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 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; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

輸出為:
技術分享
前者就是一般的輸出,root.toStyledString()比較規整。

jsoncpp構造json字符串和json數組