c + json nlohmann 과 poco json 사용
5453 단어 c/c++
가장 좋 은 c + json 라 이브 러 리 는 nlohmann 입 니 다.
https://blog.csdn.net/wphkadn/article/details/97417700
변 수 를 json 이 라 고 쓰 는 것 은 쉽 지만, json 을 변수 로 바 꾸 는 것 은 좀 복잡 해 야 하지만, nlohmann 에 대해 서 는 조금도 복잡 하지 않다.
레 퍼 런 스
http://www.it1352.com/497248.html
#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<:ptr>();
objPrint(*p, it->first);
}
else
std::cout << it->second.toString() << std::endl;
}
}
int main(int, char**)
{
typedef std::map<: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<:ptr>();
objPrint(*pObj);
return 0;
}
https://blog.csdn.net/yuhaiyang457288/article/details/47190117?utm_source=blogxgwz2
#include
#include
#include
#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<: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 << " :
" << jsnStr << "
" << std::endl;
//-------------------------- -----------------------------
JSON::Parser parse;
Dynamic::Var json = parse.parse( jsnStr );
JSON::Object::Ptr pObj = json.extract<: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();
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
3. 배열 의 성명 및 초기 화 (test 1. java)먼저 배열 의 신청, 배열 의 초기 화, 배열 의 복사 등 을 배 웠 습 니 다.배열 에 대해 저 는 C \ C + + 의 배열 과 큰 차이 가 없다 고 생각 합 니 다. 그러나 JAVA 에서 JAVA 는 대상 을 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.