This repository has been archived by the owner on Apr 25, 2018. It is now read-only.
forked from amir-sabbaghi/libjson--
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.cpp
52 lines (41 loc) · 1.61 KB
/
sample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include "json/json.h"
using namespace std;
int main()
{
cout << "sample.json test" << endl;
// reading a json file
Json input("sample.json");
// accessing object members
cout << "name: " << input["name"].as<string>() << endl;
cout << "description: " << input["description"].as<string>() << endl;
cout << "integer member: " << input["integer"].as<int>() << endl;
cout << "float member: " << input["float"].as<float>() << endl; // you should know the type of a
// member
if (input["null"].getType() == Json::JTYPE_NULL) // or you can examine it like
// this.
cout << "there is also a null member" << endl;
// iterating
cout << "array: ";
for (Json::ArrayIterator i = input["array"].arrayBegin(); i != input["array"].arrayEnd(); i++) {
cout << (*i)->as<int>() << ',';
}
cout << endl;
// creating a json
Json output;
output.setType(Json::JTYPE_OBJECT);
output["hello"] = "world";
output["number"] = 1; // note: 1 will be ambiguous, use 1.0 or 1L or 1.0f or ...
for (int i = 0; i < 10; i++) {
output["array"][i] = i + 1;
}
output["object"]["bool"] = true;
output["object"]["null"].setType(Json::JTYPE_NULL);
// writing to a file
output.save("output.json");
cout << "twitter test" << endl;
// reading a json file
Json tw("twitter_public_timeline.json");
cout << "geo: " << tw["geo"].as<string>() << endl;
return 0;
}