"Boost::property tree 를 이용한 json 스크립트 파싱"의 두 판 사이의 차이
jjuiddong
(→property_tree find() 로 내부 데이타 얻는법) |
|||
75번째 줄: | 75번째 줄: | ||
::AfxMessageBox(CString(L"Error!!\n") + str2wstr(e.what()).c_str()); | ::AfxMessageBox(CString(L"Error!!\n") + str2wstr(e.what()).c_str()); | ||
return false; | return false; | ||
+ | } | ||
+ | |||
+ | |||
+ | == boost::property_tree 데이타 구성 == | ||
+ | * http://stackoverflow.com/questions/2114466/creating-json-arrays-in-boost-using-property-trees | ||
+ | ptree pt; | ||
+ | ptree children; | ||
+ | ptree child1, child2, child3; | ||
+ | |||
+ | child1.put("childkeyA", 1); | ||
+ | child1.put("childkeyB", 2); | ||
+ | |||
+ | child2.put("childkeyA", 3); | ||
+ | child2.put("childkeyB", 4); | ||
+ | |||
+ | child3.put("childkeyA", 5); | ||
+ | child3.put("childkeyB", 6); | ||
+ | |||
+ | children.push_back(std::make_pair("", child1)); | ||
+ | children.push_back(std::make_pair("", child2)); | ||
+ | children.push_back(std::make_pair("", child3)); | ||
+ | |||
+ | pt.put("testkey", "testvalue"); | ||
+ | pt.add_child("MyArray", children); | ||
+ | |||
+ | write_json("test2.json", pt); | ||
+ | |||
+ | { | ||
+ | "testkey": "testvalue", | ||
+ | "MyArray": | ||
+ | [ | ||
+ | { | ||
+ | "childkeyA": "1", | ||
+ | "childkeyB": "2" | ||
+ | }, | ||
+ | { | ||
+ | "childkeyA": "3", | ||
+ | "childkeyB": "4" | ||
+ | }, | ||
+ | { | ||
+ | "childkeyA": "5", | ||
+ | "childkeyB": "6" | ||
+ | } | ||
+ | ] | ||
} | } |
2017년 1월 12일 (목) 16:33 판
목차 |
여러개의 속성을 가진 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; }
boost::property_tree 데이타 구성
ptree pt; ptree children; ptree child1, child2, child3; child1.put("childkeyA", 1); child1.put("childkeyB", 2); child2.put("childkeyA", 3); child2.put("childkeyB", 4); child3.put("childkeyA", 5); child3.put("childkeyB", 6); children.push_back(std::make_pair("", child1)); children.push_back(std::make_pair("", child2)); children.push_back(std::make_pair("", child3)); pt.put("testkey", "testvalue"); pt.add_child("MyArray", children); write_json("test2.json", pt);
{ "testkey": "testvalue", "MyArray": [ { "childkeyA": "1", "childkeyB": "2" }, { "childkeyA": "3", "childkeyB": "4" }, { "childkeyA": "5", "childkeyB": "6" } ] }