一、JSON描述
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the , . JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
JSON is built on two structures:
-
A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
-
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.
In JSON, they take on these forms:
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.
A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.
Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language
二、jsoncpp移植
soncpp is an implementation of a JSON ( ) reader and writer in C++. JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.
下载
或者到这里:
Linux平台编译
解压后运行如下命令:
$ sudo apt-get install scons
$ scons platform=linux-gcc
目标路径:
动态库:./libs/linux-gcc-4.8/libjson_linux-gcc-4.8_libmt.so
静态库:./libs/linux-gcc-4.8/libjson_linux-gcc-4.8_libmt.a
arm平台编译
注:platform 没有包含 arm 平台,类似 linux-gcc,所以把源码提取出来,独立编译
解压后运行如下命令:
$ mkdir arm_jsoncpp
$ cp include/ arm_jsoncpp/ -r
$ cp src/lib_json/* arm_jsoncpp/
$ cd arm_jsoncpp/
$ arm-linux-gnueabihf-g++ -c *.cpp -I./include -fPIC
$ ar cr libjsoncpp.a *.o
$ arm-linux-gnueabihf-g++ -shared -fPIC *.cpp -I./include -o libjsoncpp.so
目标路径:
动态库:./arm_jsoncpp/libjsoncpp.so
静态库:./arm_jsoncpp/libjsoncpp.a
三、json-c
JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects.
下载
或者到这里:
编译
注:此处使用 arm-linux-gnueabihf-gcc,--build 使用的是64位Linux,可以不添加此参数
解压后运行如下命令:
$ sudo apt-get install autoconf
$ sudo apt-get install automake
$ sudo apt-get install libtool
$ ./autogen.sh
$ ./configure --host=arm-linux-gnueabihf --build=i686-pc-linux
$ make
目标路径:./.libs/libjson-c.so.2.0.0
四、使用方法:
在QT在添加JSON的库路径即可
1) 从字符串读取json
#include #include #include
using namespace std;
void readStrJson();
void readStrProJson();
int main(int argc, char *argv[])
{
readStrJson();
cout << "\n\n";
readStrProJson(); return 0;
}
void readStrJson()
{
const char* str = "{\"praenomen\":\"Gaius\",\"nomen\":\"Julius\",\"cognomen\":\"Caezar\",\"born\":-100,\"died\":-44}";
Json::Reader reader;
Json::Value root;
if(reader.parse(str,root))
{
string praenomen = root["praenomen"].asString();
string nomen = root["nomen"].asString();
string cognomen = root["cognomen"].asString();
int born = root["born"].asInt();
int died = root["died"].asInt();
cout << praenomen + " " + nomen + " " + cognomen
<< " was born in year " << born
<< ", died in year " << died << endl;
}
}
void readStrProJson()
{
string strValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}";
Json::Reader reader;
Json::Value value;
if (reader.parse(strValue, value))
{
string out = value["name"].asString();
cout << out << endl;
const Json::Value arrayObj = value["array"];
for (unsigned int i = 0; i < arrayObj.size(); i++)
{
if (!arrayObj[i].isMember("cpp"))
continue;
out = arrayObj[i]["cpp"].asString();
cout << out;
if (i != (arrayObj.size() - 1))
cout << endl;
}
}
}
-
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
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
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
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
2) 从文件读取json
#include #include #include #include
using namespace std;
void readFileJson();
int main(int argc, char *argv[])
{
readFileJson();
return 0;
}
void readFileJson()
{
Json::Reader reader;
Json::Value root;
ifstream in("test.json", ios::binary);
if( !in.is_open() )
{
cout << "Error opening file\n";
return;
}
if(reader.parse(in,root))
{
string name = root["name"].asString();
int age = root["age"].asInt();
bool sex_is_male = root["sex_is_male"].asBool();
cout << "My name is " << name << endl;
cout << "I'm " << age << " years old" << endl;
cout << "I'm a " << (sex_is_male ? "man" : "woman") << endl;
string partner_name = root["partner"]["partner_name"].asString();
int partner_age = root["partner"]["partner_age"].asInt();
bool partner_sex_is_male = root["partner"]["partner_sex_is_male"].asBool();
cout << "My partner's name is " << partner_name << endl;
cout << (partner_sex_is_male ? "he" : "she") << " is " << partner_age << " years old" << endl;
cout << "Here's my achievements:" << endl;
for(unsigned int i = 0; i < root["achievement"].size(); i++)
{
string ach = root["achievement"][i].asString();
cout << ach << '\t';
}
cout << endl;
cout << "Reading Complete!" << endl;
}
else
{
cout << "parse error\n" << endl;
}
in.close();
}
-
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
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
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
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
test.json文件内容如下:
{
"name":"Mike Jiang",
"age":23,
"sex_is_male":true,
"partner":
{
"partner_name":"Galatea",
"partner_age":21,
"partner_sex_is_male":false
},
"achievement":["ach1","ach2","ach3"]
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
3) 将信息保存到json文件
#include #include #include #include
using namespace std;
void writeFileJson();
int main(int argc, char *argv[])
{
writeFileJson();
return 0;
}
void writeFileJson()
{
Json::Value root;
root["name"] = Json::Value("Mike Jiang");
root["age"] = Json::Value(23);
root["sex_is_male"] = Json::Value(true);
Json::Value partner;
partner["partner_name"] = Json::Value("Galatea");
partner["partner_age"] = Json::Value(21);
partner["partner_sex_is_male"] = Json::Value(false);
root["partner"] = Json::Value(partner);
root["achievement"].append("ach1");
root["achievement"].append("ach2");
root["achievement"].append("ach3");
cout << "FastWriter:" << endl;
Json::FastWriter fw;
cout << fw.write(root) << endl << endl;
cout << "StyledWriter:" << endl;
Json::StyledWriter sw;
cout << sw.write(root) << endl << endl;
ofstream os;
os.open("demo.json");
os << sw.write(root);
os.close();
}
-
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
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
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
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
7