Jsoncpp的使用详细教程

Jsoncpp的使用详细教程

本文详细介绍Jsoncpp的入门知识以及下载、使用。具体实现基于linux系统。笔者的系统为CentOS 7。

Jsoncpp简介

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语言。

JSON建构于两种结构:
“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。

值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。

这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。

Jsoncpp的数据形式

JSON具有以下这些数据形式:
对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。
这里写图片描述

数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。
这里写图片描述

值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。
这里写图片描述

字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。字符串(string)与C或者Java的字符串非常相似。
这里写图片描述

数值(number)也与C或者Java的数值非常相似。除去未曾使用的八进制与十六进制格式。除去一些编码细节。空白可以加入到任何符号之间。
这里写图片描述

Jsoncpp的安装

C++要使用JSON来封装和解析数据,一般采用jsoncpp. 在其GitHub主页可以进行下载:https://github.com/open-source-parsers/jsoncpp
如在linux系统下,可以使用如下命令

git clone –branch=master https://github.com/open-source-parsers/jsoncpp

将jsoncpp下载到当前文件夹下。

jsoncpp可以采用scons进行编译,因此需要先安装scons。需要注意的是,Scons需要安装了Python才能使用。linux下安装Python的步骤此处不在赘述。我们假设系统中已经安装了Python,可以采用下面的命令获取Scons:

wget http://prdownloads.sourceforge.NET/scons/scons-2.2.0.tar.gz

Scons以及Jsoncpp具体的安装步骤如下:

#tar -zxvf scons-2.2.0.tar.gz
#cd scons-2.2.0
#python setup.py install
#tar -zxvf jsoncpp.tar.gz
#cd jsoncpp
#scons platform=linux-gcc
#cp libs/linux-gcc-4.8.5/libjson_linux-gcc-4.8.5_libmt.so /lib
#ln /lib/libjson_linux-gcc-4.8.5_libmt.so /lib/libjson.so
#cp include/json/ /usr/include/
#ldconfig
#./bin/linux-gcc-4.8.5/test_lib_json

【注意这里的版本号可能不同】先通过cd libs进入libs目录,再用“ls”命令查看自己的版本号,然后在上述步骤中修改成自己的版本
号即可。
执行上述命令之后,屏幕出现下列提示信息:

Testing CharReaderAllowSpecialFloatsTest/issue209: OK
Testing BuilderTest/settings: OK
Testing IteratorTest/distance: OK
Testing IteratorTest/names: OK
Testing IteratorTest/indexes: OK
Testing IteratorTest/const: OK
Testing RValueTest/moveConstruction: OK
All 57 tests passed
由此说明Jsoncpp已经顺利安装完成。

Jsoncpp使用示例

例1:
程序test1.cpp代码如下:

#include <json/json.h>  //需要在程序中添加json.h头文件
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  ifstream ifs;
  ifs.open("testjson.json");
  if(!ifs.is_open())
  {
    cout<<"open file error!"<<endl;
    return 1;
  }

  Json::Reader reader;
  Json::Value root;
  if (!reader.parse(ifs, root, false))
  {
    return -1;
  }

  std::string name = root["name"].asString();
  int age = root["age"].asInt();

  std::cout<<name<<std::endl;
  std::cout<<age<<std::endl;

  return 0;
}

文件testjson.json中的内容如下:
这里写图片描述
使用如下命令对程序进行编译:

g++ -o test1 test1.cpp -ljson

执行编译得到的文件得到如下结果:
这里写图片描述

例2,json写入:
程序test2.cpp代码如下:

#include<json/json.h>
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
  Json::Value root;
  Json::FastWriter writer;
  Json::Value person;

  person["name"] = "hello world";
  person["age"] = 100;
  root.append(person);

  std::string json_file = writer.write(root);


  ofstream ofs;
  ofs.open("test2.json");
  if(!ofs.is_open())
  {
    cout<<"open file error!"<<endl;
    return -1;
  }
  ofs<<json_file;
  ofs.close();
  return 0;
}

将程序编译后,执行。得到文件test2.json中的内容如下:
[{“age”:100,”name”:”hello world”}]


本文部分整理自博客:

猜你喜欢

转载自blog.csdn.net/xiaolong361/article/details/77017189