C++使用nlohmann json
最好用的c++ json库是 nlohmann
C++使用nlohmann json_wphkadn的博客-CSDN博客
把变量写成json容易,可是把json变成变量就要复杂一点,不过对于nlohmann一点都不复杂
mkdir build
cd build
cmake ..
make
sudo make install
boost::property_tree::read_json
C++ boost 解析 Json_「已注销」的博客-CSDN博客
参考
如何使用Poco C ++创建和解析分层/嵌套JSON? - IT屋-程序员软件开发技术分享社区
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/Stringifier.h"
using Poco::JSON::ParseHandler;
using Poco::JSON::Stringifier;
using Poco::JSON::Object;
using Poco::JSON::Parser;
using Poco::Dynamic::Var;
using Poco::DynamicStruct;
void objPrint(Object& obj, const std::string& name = "")
{
for (Object::ConstIterator it = obj.begin(),
end = obj.end(); it != end; ++it)
{
if (!name.empty()) std::cout << name << ':';
std::cout << it->first << ':';
if (it->second.type() == typeid(Object::Ptr))
{
Object::Ptr p = it->second.extract<Object::Ptr>();
objPrint(*p, it->first);
}
else
std::cout << it->second.toString() << std::endl;
}
}
int main(int, char**)
{
typedef std::map<std::string, int> PersonMap;
std::string timeStamp = "2015-05-14T17:47:21.999Z";
Poco::Int32 identifier = 3;
std::string name = "john smith";
PersonMap metaData;
metaData.insert(PersonMap::value_type("william", 45));
Object obj(true);
obj.set("ts", timeStamp);
obj.set("name", name);
obj.set("identifier", identifier);
Object::Ptr pMD = new Poco::JSON::Object(true);
for (PersonMap::iterator it = metaData.begin(),
end = metaData.end(); it != end; ++it)
{
pMD->set("middle_name", it->first);
pMD->set("age", it->second);
}
obj.set("metadata", pMD);
std::ostringstream os;
obj.stringify(os, 2);
std::cout << os.str() << std::endl;
Parser parser;
Var result = parser.parse(os.str());
Object::Ptr pObj = result.extract<Object::Ptr>();
objPrint(*pObj);
return 0;
}
poco JSON 的使用_yuhaiyang457288的专栏-CSDN博客
#include<iostream>
#include <stdint.h>
#include <sstream>
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/StreamCopier.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/JSON/Query.h"
#include "Poco/JSON/PrintHandler.h"
using namespace Poco::Dynamic;
using namespace Poco;
using std::string;
// json 的简单示例。
void JsonGet( )
{
string jsonString = "{ \"name\" : \"yuhaiyang\" }";
JSON::Parser parser;
Dynamic::Var result;
parser.reset();
result = parser.parse( jsonString );
JSON::Object::Ptr pObj = result.extract<JSON::Object::Ptr>();
Dynamic::Var ret = pObj->get( "name2" );
if ( ret.isEmpty() )
{
std::cout << "is null" << std::endl;
}else
{
std::cout << ret.toString() << std::endl;
}
}
//在 json 里加入数组。
void JsonArry( void )
{
JSON::Object jsnObj;
JSON::Array jsnArry;
JSON::Object subObj1;
JSON::Object subObj2;
jsnObj.set( "command", "createuser" );
subObj1.set( "name", "yuhaiyang");
subObj1.set( "pass", "123" );
subObj2.set( "name", "cj" );
subObj2.set( "pass", "456" );
jsnArry.add( subObj1 );
jsnArry.add( subObj2 );
jsnObj.set( "userinfo", jsnArry );
std::stringstream jsnString;
jsnObj.stringify( jsnString, 3 );
std::cout << jsnString.str() << std::endl;
}
//解析数据
void JsonGetArry( void )
{
/*-------------------构造包含Array的JSON(string类型)--------------------------------*/
JSON::Object jsnObj;
JSON::Array jsnArry;
JSON::Object subObj1;
JSON::Object subObj2;
jsnObj.set( "command", "createuser" );
subObj1.set( "name", "yuhaiyang");
subObj1.set( "pass", "123" );
subObj2.set( "name", "cj" );
subObj2.set( "pass", "456" );
jsnArry.add( subObj1 );
jsnArry.add( subObj2 );
jsnObj.set( "userinfo", jsnArry );
std::stringstream jsnOstream;
jsnObj.stringify( jsnOstream, 3 );
string jsnStr = jsnOstream.str();
std::cout << "原数据:\n" << jsnStr << "\n\n\n" << std::endl;
//--------------------------解析数组-----------------------------
JSON::Parser parse;
Dynamic::Var json = parse.parse( jsnStr );
JSON::Object::Ptr pObj = json.extract<JSON::Object::Ptr>();
JSON::Array::Ptr pArry = pObj->getArray( "userinfo" );
JSON::Array::ConstIterator it = pArry->begin();
//把数组里的所有内容打印出来
//当然也可以把每个对象拿出来用。
for ( ; it != pArry->end(); it++ )
{
std::cout << it->toString() << std::endl;
}
}
int main()
{
JsonGet();
JsonArry();
JsonGetArry();
}