-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.cpp
More file actions
38 lines (32 loc) · 854 Bytes
/
json.cpp
File metadata and controls
38 lines (32 loc) · 854 Bytes
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
#include<cstdio>
#include<iostream>
#include<string>
#include<jsoncpp/json/json.h>
int main()
{
std::string name="小明";
std::string sex="男";
int age = 26;
Json::Value value;
value["姓名"] = name;
value["性别"] = sex;
value["年龄"] = age;
value["成绩"].append(88);
value["成绩"].append(77);
value["成绩"].append(99);
Json::StyledWriter writer;
std::string json_str = writer.write(value);
printf("json_str:[%s]\n",json_str.c_str());
Json::Value val2;
Json::Reader reader;
reader.parse(json_str,val2);
std::cout << "name:" <<val2["姓名"] <<std::endl;
std::cout << "sex:" <<val2["性别"] <<std::endl;
std::cout << "age:" <<val2["年龄"].asInt() <<std::endl;
std::cout << "score:" <<val2["成绩"] <<std::endl;
for(auto it : val2["成绩"])
{
std::cout << "ch score:" << it << std::endl;
}
return 0;
}