"Boost::property tree 를 이용한 json 스크립트 파싱"의 두 판 사이의 차이

jjuiddong
이동: 둘러보기, 찾기
(여러개의 속성을 가진 child 정보를 가져오는 방법)
(property_tree find() 로 내부 데이타 얻는법)
39번째 줄: 39번째 줄:
  
 
== property_tree find() 로 내부 데이타 얻는법 ==
 
== property_tree find() 로 내부 데이타 얻는법 ==
 +
using boost::property_tree::ptree;
 
  ptree props;
 
  ptree props;
 
  boost::property_tree::read_json(fileName.c_str(), props);
 
  boost::property_tree::read_json(fileName.c_str(), props);

2017년 1월 7일 (토) 12:36 판

목차

여러개의 속성을 가진 child 정보를 가져오는 방법

	"property" : [
		{
			"symbolname" : "CLobbyServer#1"
		},
		{
			"symbolname" : "CLobbyServer#1"
		}
	]

boost::property_tree 를 이용한 json 스크립트 파싱

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

using boost::property_tree::ptree;
boost::property_tree::read_json("filename", props);
ptree &children = props.get_child("property");
BOOST_FOREACH(ptree::value_type &vt, children)
{
	const string name = vt.second.get<string>("symbolname");
	... 
}

다른방식의 json 파싱

json 은 key, value 값으로 구성된 스크립트지만, 다음처럼 value값이 없이도 사용할 수 있다.

"collapse" : [
	"basic::s2c",  
	"login::s2c"
]

이 스크립트의 정보를 가져오는 방법은 다음과 같다. (이 예제의 특징은 key value 만 있고, data value는 없다는 점이다.)

ptree &collapse = g_Props.get_child("collapse");
BOOST_FOREACH(auto &child, collapse)
{
	if ("basic::s2c" == child.second.data())
		return true;
}

property_tree find() 로 내부 데이타 얻는법

using boost::property_tree::ptree;
ptree props;
boost::property_tree::read_json(fileName.c_str(), props);
ptree &children = props.get_child("extract");
BOOST_FOREACH(auto &vt, children)
{
	ptree::assoc_iterator fit = vt.second.find("field");
	if (vt.second.not_found() != fit)
	{
		ptree &child_field = vt.second.get_child("field");
		BOOST_FOREACH(auto &v, child_field)
		{
			Jjuiddong (토론)
		}
	}
}

boost::property_tree 데이타 저장

  • 이미 존재하는 scale 키 에 sens 값을 업데이트 한다.
try
{
	// boost property tree
	using boost::property_tree::ptree;
	using std::string;
	ptree props;
	boost::property_tree::read_json(fileName, props);
 
	props.put<float>("scale", sens);

	boost::property_tree::write_json(fileName + "1", props);
}
catch (std::exception&e)
{
	::AfxMessageBox(CString(L"Error!!\n") + str2wstr(e.what()).c_str());
	return false;
}
개인 도구
이름공간

변수
행위
둘러보기
도구모음