使用jsoncpp解析JSON数据

使用jsoncpp解析JSON数据

(转自http://www.cppfans.org/1445.html,谢谢作者的分享)
1.jsoncpp是什么?

jsoncpp是一个使用C++语言来解析json文件的开源库,其项目地址为:http://sourceforge.net/projects/jsoncpp/,属于免费项目,任何人都可以下载使用

2. 编译jsoncpp

jsoncpp文件中提供了vs71的工程文件以及makerelease.py文件,用来编译,里面分为jsontest、lib_json、test_lib_json三个工程,按照自己需要的编译。

注意:如果使用VS认的编译选项MTd或者MT,在使用json_libmtd.lib的时候可能会出现LNK2038错误(我使用的VS2012 vc110环境)所以请修改MTD为MDd,MT为MD

3.使用jsoncpp读JSON文件

如何将lib库添加进VS工程中在此就不赘述了。先看第一个文件的例


// JSON文件
{ "address" : [
"name" : "eliteYang" , "email" "elite_yang@163.com" } Crayon-sy" style="border:0px; margin:0px; padding:0px; font-family:inherit; font-size:inherit!important; line-height:inherit!important; height:inherit!important; color:rgb(51,
"AAA" "aaa@163.com" Crayon-sy" style="border:0px; margin:0px; padding:0px; font-family:inherit; font-size:inherit!important; line-height:inherit!important; height:inherit!important; color:rgb(51,
"BBB" "bbb@163.com" }
] }

/**
* file : jsoncpp_test.cpp
* author : eliteYang
* email: elite_yang@163.com
* blog : http://www.cppfasn.org
* desc : json cpp test
*/
#include <fstream>
#include <string>
#include "jsoncpp/json.h"
int _tmain ( argc _TCHAR * argv [ )
{
std :: ifstream ifs ;
ifs . open ( "test.json" ) ;
Json :: Reader reader ;
:: Value root ;
if ( ! reader . parse ( ifs false )
{ return - 1 ; }
add_value = root [ "address" ;
for i 0 i < add_value . size ( ++ i )
{
temp_value [ i ;
string strName temp_value "name" . asstring ;
strMail "email" ;
:: cout << "name: " strName " email: " strMail :: endl ;
// use value array[index]
//Json::Value temp_value = add_value[i];
//std::string strName = add_value[i]["name"].asstring();
//std::string strMail = add_value[i]["email"].asstring();
//std::cout << "name: " << strName << " email: " << strMail << std::endl;
}
system "Pause" ;
return ;
结果:

name : eliteYang email elite_yang @ 163.com
AAA email aaa 163.com
BBB email bbb 163.com
请按任意键继续 . .

跟我们文件中的数据完全一致。

4.使用JSON写入一块数据

我们继续使用上述文件,在中间加上一块数据。我们插入一个新的{"name": "append","email": "append@163.com"}

/**
* file : jsoncpp_test.cpp
* author : eliteYang
* email: elite_yang@163.com
* blog : http://www.cppfasn.org
* desc : json cpp test
*/
#include <fstream>
#include <string>
#include "jsoncpp/json.h"
)
{
;
;
;
;
)
}
Value & ;
append_value ;
append_value ] "append" ;
"append@163.com" ;
. append ( append_value ;
)
{
;
;
;
;
}
:: FastWriter writer ;
json_append_file writer . write ( root ;
:: ofstream ofs ;
ofs "test_append.json" ;
ofs json_append_file ;
;
;

163.com
163.com
163.com
append email append 163.com

// test_append.json
Crayon-line Crayon-striped-line" id="Crayon-5276495a5ecf6471688712-2" style="border:0px; margin:0px; padding:0px 5px; font-family:monospace; background-color:rgb(248, Crayon-s" style="border:0px; margin:0px; padding:0px; font-family:inherit; font-size:inherit!important; line-height:inherit!important; height:inherit!important; color:rgb(221, 当然了,jsoncpp对数组的解析也支持STL中迭代器的风格,不过我个人觉得还是数组好理解一些。迭代器的解析风格就不写了,大家可以自己摸索下,主要是使用Json::Value::Members。

JSON官方还是非常推荐用jsoncpp来解析JSON文件的,大家也看到了,确实比较方便。

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...