码迷,mamicode.com
首页 > 编程语言 > 详细

C++解析JSON(jsonCpp)

时间:2015-05-12 09:25:54      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:json   c++   

C++解析JSON(jsonCpp)

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

准备工作

下载jsonCpp

我们首先新建一个文件夹jsonCpp,然后将jsoncpp克隆到我们的文件夹下面:

>mkdir jsonCpp && cd jsonCpp
>git clone https://github.com/open-source-parsers/jsoncpp.git

现在你的文件夹下面就会有一个jsoncpp文件夹,这里面就是jsoncpp的源码
技术分享

编译jsoncpp

面对这个多个文件和文件夹,是不是有点混乱了,但是我们用到的东西就只有两个文件夹,一个是src/lib_json文件夹,一个是include/json文件夹
技术分享

于是新建一个目录,并将这两个文件夹复制出来,将json文件夹复制到lib_json里面
技术分享

下载我们就需要编译这些文件,并生成静态链接库,其实不编译也行,只是后面用起来方便一点,我们来编辑一个Makefile

TARGET=libjson.lib

SRCS=json_writer.cpp     json_reader.cpp     json_value.cpp
OBJS=$(SRCS:.cpp=.o)
INCS=-I json

$(TARGET):$(OBJS)
    ar rv $@ $^

%.o:%.cpp
    g++ -c $< $(INCS) -o $@

make 以后发现报错了

显示错误为:
技术分享

报错找不到头文件
博主琢磨了一番,发现还是找不到解决方案(对makefile不太熟悉,有大神指点一下么?),只有改源文件了了,于是将json_value.cppjson_reader.cpp,json_write.cpp这三个文件里面的头文件的尖括号改为双引号,然后make编译成功
技术分享

读取JSON文件

我们得到了jsonCpp的静态链接库libjson.lib,接下面我们开始用jsoncpp解析json文件了
从上面的编译可知,jsoncpp里面大致包含三个类:

  • Json::Value 用来保存读取的数据或者将要写入文件的数据
  • Json::Reader 用来读取JSON文件里面的数据,并传入到Json::Value对象里面去
  • Json::FastWriter 用来将Json::Value对象写入到一个JSON文件中去

读取简单的JSON文件

include <fstream>
#include <cassert>
#include "json/json.h"
#include <iostream>

using namespace std;

int main()
{
    ifstream inFile("test.json", ios::in);
    if(!inFile.is_open())
    {
        cerr<<"Open the file failed"<<endl;
        return -1;
    }

    Json::Value root;
    Json::Reader reader;
    if(!reader.parse(inFile , root , false))
    {
        cerr<<"parse file failed"<<endl;
        return -1;
    }

    cout<<"name : "<<root["name"].asString()<<endl;
    cout<<"age : "<<root["age"].asInt()<<endl;
    return -1;
}

test.json里面的数据为:

{
    "name":"qeesung",
    "age":21
}

我们编译一下:g++ main.cpp libjson.lib -o myJson
运行结果为:
技术分享

读取含有数组的JSON文件

#include <fstream>
#include <cassert>
#include "json/json.h"
#include <iostream>

using namespace std;

int main()
{
    ifstream inFile("test.json", ios::in);
    if(!inFile.is_open())
    {
        cerr<<"Open the file failed"<<endl;
        return -1;
    }

    Json::Value root;
    Json::Reader reader;
    if(!reader.parse(inFile , root , false))
    {
        cerr<<"parse file failed"<<endl;
        return -1;
    }
    for(int i = 0 ; i<root.size(); ++i)
    {
        cout<<"name : "<<root[i]["name"].asString()<<endl;
        cout<<"age : "<<root[i]["age"].asInt()<<endl;
    }
    return -1;
}

json文件为:

[
    {"name":"qeesung1","age":21},
    {"name":"qeesung2","age":22},
    {"name":"qeesung3","age":23},
    {"name":"qeesung4","age":24}
]

编译运行结果为:
技术分享

写入数据到SON文件中

#include <fstream>
#include <cassert>
#include "json/json.h"
#include <iostream>
using namespace std;
int main()
{
    ofstream outFile("test.json", ios::out | ios::trunc);
    if(!outFile.is_open())
    {
        cerr<<"Open the file failed"<<endl;
        return -1;
    }

    Json::Value root;
    Json::FastWriter writer;
    for(int k = 0 ; k < 3 ; ++k)
    {
        root[k]["root"]="qeesung";
        root[k]["age"]=k+10;
    }
    string str = writer.write(root);
    outFile<<str;
    return -1;
}

我们得到结果:

[{"age":10,"root":"qeesung"},{"age":11,"root":"qeesung"},{"age":12,"root":"qeesung"}]

C++解析JSON(jsonCpp)

标签:json   c++   

原文地址:http://blog.csdn.net/ii1245712564/article/details/45652577

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!