Server/C++
JSON 사용하기 - nlohmann json
Juzdalua
2024. 11. 20. 11:49
1. 헤더파일 다운로드
json.hpp 파일을 다운로드한다.
https://github.com/nlohmann/json/releases
Releases · nlohmann/json
JSON for Modern C++. Contribute to nlohmann/json development by creating an account on GitHub.
github.com
2. VS 세팅
편의성을위해 다운로드 받은 헤더파일을 위치를 옮겼다.
프로젝트 속성 - C/C++ - 일반 - 추가 포함 디렉터리
3. 예제
#include <json.hpp>
using json = nlohmann::json;
void TestJSON()
{
json j = {
{"name", "John"},
{"age", 30},
{"city", "New York"}
};
cout << j.dump() << endl; // JSON -> string
// JSON 역직렬화
string s = R"({"name": "Alice", "age": 25})";
json j2 = json::parse(s); // string -> JSON
cout << "Name: " << j2["name"] << ", Age: " << j2["age"] << endl;
}