Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30103827
  • 博文数量: 230
  • 博客积分: 2868
  • 博客等级: 少校
  • 技术积分: 2223
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-08 21:48
个人简介

Live & Learn

文章分类

全部博文(230)

文章存档

2022年(2)

2019年(5)

2018年(15)

2017年(42)

2016年(24)

2015年(13)

2014年(1)

2012年(5)

2011年(58)

2010年(56)

2009年(9)

我的朋友

分类: LINUX

2016-09-11 08:55:28

一、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).

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.

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.

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平台编译

解压后运行如下命令:

# 先安装 scons 
$ 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,可以不添加此参数
解压后运行如下命令:

# autoconf automake libtool 必须安装好 
$ 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(); //从字符串中读取JSON 
void readStrProJson(); //从字符串中读取JSON(内容复杂些) 
int main(int argc, char *argv[]) 
 { 
      readStrJson();
       cout << "\n\n";
    readStrProJson(); return 0;
 } //从字符串中读取JSON 
void readStrJson() 
 { 
//字符串
 const char* str = "{\"praenomen\":\"Gaius\",\"nomen\":\"Julius\",\"cognomen\":\"Caezar\",\"born\":-100,\"died\":-44}";
  /*
  // json内容如下:
  {
        "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; 
 }
 } //从字符串中读取JSON(内容复杂些) 
void readStrProJson() 
 string strValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}"
  /*
    //json内容如下:
    {
    "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(); //从文件中读取JSON,一个存储了JSON格式字符串的文件 
int main(int argc, char *argv[]) 
 { 
 readFileJson(); 
return 0;
 } 
//从文件中读取JSON 
void readFileJson()
 {
 Json::Reader reader;
 Json::Value root; 
//从文件中读取,保证当前文件有test.json文件
 ifstream in("test.json", ios::binary); 
// 这个"test.json"文件的路径应该是在exe文件的上一级目录,即Makefile所在的目录 
//in.open("test.json", ios::binary); 
if( !in.is_open() )
 {
  cout << "Error opening file\n";
  return;
 } 
/*
    //test.json内容如下:
    {
        "name":"Tsybius",
        "age":23,
        "sex_is_male":true,
        "partner":
        {
            "partner_name":"Galatea",
            "partner_age":21,
            "partner_sex_is_male":false
        },
        "achievement":["ach1","ach2","ach3"]
    }
    */ 
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();
//将信息保存为JSON格式
 int main(int argc, char *argv[])
 {
 writeFileJson(); 
return 0;
 } 
//将信息保存为JSON格式
 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();
  /*
    //json文件内容如下:
 {
        "achievement" : [ "ach1", "ach2", "ach3" ], 
 "age" : 23, 
 "name" : "Mike Jiang",
 "partner" : 
{
            "partner_age" : 21,
 "partner_name" : "Galatea",
 "partner_sex_is_male" : false 
 }, 
 "sex_is_male" : true 
 }
 */ 
}
  • 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



阅读(1359) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~