프로그래밍/기타 라이브러리
[jsoncpp] json 생성,파싱하기.
친루엄
2018. 2. 25. 21:44
[라이브러리 설치법.]
http://cinrueom.tistory.com/4?category=791378
[환경]
windows 10 Pro
Visual Studio Community 2017
[Json 생성하기.]
프로젝트를 생성하고, 헤더파일과 라이브러리 파일을 추가하자.
경로는 알아서...
#include "jsoncpp\include\json\json.h" #pragma comment(lib,"jsoncpp\\lib\\lib_json.lib")
풀코드를 봅시다.
#include <iostream> #include <fstream> #include "jsoncpp\include\json\json.h" #pragma comment(lib,"jsoncpp\\lib\\lib_json.lib") #pragma warning(disable: 4996) //error C4996 뜨는 경우 using namespace std; int main() { string str; Json::Value root; root["name"] = "KKK"; root["age"] = 12; root["address"] = "kor"; root["gfriend"] = true; Json::Value family; family.append("mother"); family.append("father"); family.append("brother"); root["family"] = family; Json::StyledWriter writer; str = writer.write(root); cout << str << endl ; std::ofstream ost("test.json"); ost << str; getchar(); return 0; }
결과
[Json 파싱하기.]
ifstream ist("test.json"); string str; for (char p; ist >> p;) str += p; Json::Reader reader; Json::Value root; bool parsingRet = reader.parse(str, root); cout << root["name"] << endl; cout << root["age"] << endl; cout << root["address"] << endl; cout << root["gfriend"] << endl; Json::Value family = root["family"]; cout << family[0].asString() << endl; cout << family[1].asString() << endl; cout << family[2].asString() << endl;
결과
[Jsoncpp 도큐먼트]
http://jsoncpp.sourceforge.net/annotated.html