jsoncpp 源码编译与简单使用

******************************************************
标准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