jsoncpp 源码编译与简单使用

本文详细介绍如何在VC2012 MFC及Qt5.2环境下编译并使用jsoncpp源码,包括源码下载、配置步骤、常见错误解决方法及简单示例。通过本文可以快速上手jsoncpp进行JSON数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

******************************************************
标准C++实现jsoncpp 源码使用编译
(VC2012 MFC)
(Qt5.2 Widget)
******************************************************
1)下载jsoncpp源代码:jsoncpp-src-0.5.0.tar.gz
2)解压...
3)需要的源代码文件:jsoncpp-src-0.5.0/include/json:
(1)autolink.h
(2)config.h
(3)features.h
(4)forwards.h
(5)json.h
(6)reader.h
(7)value.h
(8)writer.h
jsoncpp-src-0.5.0/src/lib_json:
(9) json_batchallocator.h
(10)json_internalarray.inl
(11)json_internalmap.inl
(12)json_reader.cpp
(13)json_value.cpp
(14)json_valueiterator.inl
(15)json_writer.cpp
(16)sconscript

4)把16个文件集中放到一个文件夹"json"中,把文件夹json放到工程目录下。
5)把所有文件添加到系统工程中
6)编译报错:在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"”
解决办法1:在cpp文件头部添加 #include "stdafx.h"
解决办法2:解决方案标签/选中cpp文件/右键/属性/cc++/禁用预编译头
该问题只针对VC中出现。
7)几个头文件包含需要修改一下:目录修改、<>改为""等
8)编译成功,包含json.h即可使用,注意命名空间Json的使用

************************************************************
josncpp 简单使用(Qt例子)
************************************************************
// 从字符串解析json
const char* str = "{\"x\":[{\"aaa\":\"bbb\"}]}";
Json::Reader reader;
Json::Value root;
if (reader.parse(str, root)) // reader将Json字符串解析到root,root将包含Json里所有子元素
{
std::string upload_id = root["x"][(Json::UInt)0]["aaa"].asString();
QString strTTT ;
strTTT+=upload_id.c_str();
QMessageBox::information(this,"提示",strTTT);
}

// json对象转化为字符串
Json::FastWriter writerObj;
QString strT;
strT+=writerObj.write(root).c_str();
QMessageBox::information(this,"提示",strT);

************************************************************
附:
json串在线验证: http://www.json.cn/
************************************************************

转载于:https://www.cnblogs.com/Esperanto/p/6027375.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值