Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONCPP Writing to files

Tags:

c++

json

jsoncpp

JSONCPP has a writer, but all it seems to do is get info from the parser and then output it into a string or a stream. How do I make it alter or create new objects, arrays, values, strings, et cetera and write them into the file?

like image 526
Yelnats Avatar asked Nov 27 '10 03:11

Yelnats


People also ask

How do I read a JSON file in CPP?

Here's the code: #include <json/value. h> #include <fstream> std::ifstream people_file("people. json", std::ifstream::binary); people_file >> people; cout<<people; //This will print the entire json object. //The following lines will let you access the indexed objects.


1 Answers

#include<json/writer.h> 

Code:

    Json::Value event;        Json::Value vec(Json::arrayValue);     vec.append(Json::Value(1));     vec.append(Json::Value(2));     vec.append(Json::Value(3));      event["competitors"]["home"]["name"] = "Liverpool";     event["competitors"]["away"]["code"] = 89223;     event["competitors"]["away"]["name"] = "Aston Villa";     event["competitors"]["away"]["code"]=vec;      std::cout << event << std::endl; 

Output:

{         "competitors" :          {                 "away" :                  {                         "code" : [ 1, 2, 3 ],                         "name" : "Aston Villa"                 },                 "home" :                  {                         "name" : "Liverpool"                 }         } } 
like image 114
cegprakash Avatar answered Sep 18 '22 16:09

cegprakash