【JSONCpp】简介及demo

一、JSON简介

JSON

一种轻量级的数据交换格式,易于阅读、编写、解析,全称为JavsScript ObjectNotation。

JSON由两种基本结构组成

①   名字/值 对的集合,可以理解为对象

②   值的组合, 可以理解为数组

 

示例

string strTemp =
        "{ \"name\" : \"cuihao\" ," \ " \"age\" : 28 }"; string strRoot = 
        "{ \"key_string\" : \"value_string\", " \ " \"key_number\" : 28, " \ " \"key_boolean\" : false, " \ " \"key_double\" : 12.345, " \ " \"key_object\" : json_temp, " \ " \"key_array\" : [\"array_string1\", \"array_string2\", 12345]}";

 

 

 

 

二、JSONCPP

1.     JsonCPP简介

     jsoncpp是c++解析JSON串常用的解析库之一。其常用的类有:

a)     Json::Value    可以表示里所有的类型,比如int,string,object,array等,其支持的类型可以参考Json:ValueType中的值。

b)     Json::Reader  将json文件流或字符串解析到Json::Value,主要函数有Parse。

c)     Json::Writer   与Json::Reader相反,将Json::Value转化成字符串流,注意它的两个子类:Json::FastWriter和Json::StyleWriter,分别输出不带格式的json和带格式的json。

d)     Json::Value::Members主要用于以STL风格解析JSON数组。看过源代码的人已知道,Members其实是typedefvector而已。

 

 

在VC中使用JSONCPP

下载源码后,进入D:\SourceSoftware\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\makefiles\vs71类似目录用VS打开.sln文件进行编译。

注意:

Debug下可以直接编译

Release下:将lib_json项目属性:配置属性—C/C++--输出文件:汇编输出选择【无列表】,应为为No List。

 

使用JSONCPP时,添加

头文件目录:D:\SourceSoftware\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\include

库目录:

Debug: D:\SourceSoftware\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\build\vs71\debug\lib_json

#pragma  commebt(lib, “json_vc71_libmtd.lib”) 对应/MDd

 

Release: D:\SourceSoftware\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\build\vs71\release\lib_json

#pragma  commebt(lib, “json_vc71_libmt.lib”)  对应/MT

 

 

 

三、上代码

 

#include "stdafx.h" #include <iostream> #include <string> #include <json\json.h> 
using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string strStudent = "{ \"name\" : \"cuishihao\", \"age\" : 28, \"major\" : \"cs\" }"; //char* strStudent = "{ \"name\" : \"cuishihao\", \"age\" : 28, \"major\" : \"cs\" }";
 Json::Reader reader; Json::Value value; bool bRet = reader.parse(strStudent, value); if (false == bRet) { cerr << endl << "read value error \n"; return -1; } cout << value["name"].asString()<<endl; cout << value["age"].asInt()<<endl; cout << value["major"].asString()<<endl; cout << endl; Json::Value json_temp; json_temp["name"] = Json::Value("cuihao"); json_temp["age"] = Json::Value(28); Json::Value root; root["key_string"] = Json::Value("value_string"); root["key_number"] = Json::Value(12345); root["key_boolean"] = Json::Value(true); root["key_double"] = Json::Value(12.345); root["key_object"] = json_temp; root["key_array"].append("array_string1"); root["key_array"].append("array_string2"); root["key_array"].append(12345); Json::ValueType type = root.type(); Json::Value arrValue = root["key_array"]; for (Json::Value::UInt i = 0; i < arrValue.size(); ++ i) { if (arrValue[i].isString()) cout << arrValue[i].asString(); else if (arrValue[i].isInt()) cout << arrValue[i].asInt(); cout << endl; } cout << endl << "----------------------------------- " <<endl; string strTemp =
        "{ \"name\" : \"cuihao\" ," \ " \"age\" : 28 }"; string strRoot = 
        "{ \"key_string\" : \"value_string\", " \ " \"key_number\" : 28, " \ " \"key_boolean\" : false, " \ " \"key_double\" : 12.345, " \ " \"key_object\" : json_temp, " \ " \"key_array\" : [\"array_string1\", \"array_string2\", 12345]}"; Json::StyledWriter writer; cout << endl << writer.write(root) << endl; cout << endl << "----------------------------"<<endl; Json::Value::Members members = root.getMemberNames(); for(Json::Value::Members::const_iterator iter = members.begin(); iter != members.end(); ++ iter) { string strName = *iter; if (root[strName].isInt()) cout << root[strName].asInt(); else if (root[strName].isString()) cout << root[strName].asString(); else if (root[strName].isDouble()) cout << root[strName].asDouble(); else if(root[strName].isBool()) cout << root[strName].asBool(); else if(root[strName].isArray()) { for(Json::Value::ArrayIndex i = 0; i < root[strName].size(); ++ i) { if(root[strName][i].isInt()) cout << root[strName][i].asInt(); else if(root[strName][i].isString()) cout << root[strName][i].asString(); else cout << "others"; cout << endl; } } else if (root[strName].isObject()) { Json::Value::Members mbs = root[strName].getMemberNames(); for (Json::Value::Members::const_iterator iter2 = mbs.begin(); iter2 != mbs.end(); ++ iter2) { string strName2 = *iter2; if(root[strName][strName2].isInt()) cout << root[strName][strName2].asInt(); else if(root[strName][strName2].isString()) cout << root[strName][strName2].asString(); else cout << "others"; cout << endl; } //for
        } //else if
        else cout << "others"; cout << endl; } return 0; }

 

 

 

执行结果

 

 

  

 

转载于:https://my.oschina.net/u/926461/blog/350934

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值