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;
}

'Server > C++' 카테고리의 다른 글

C++ 환경변수 .env 파일 사용하기  (0) 2025.01.03
C++ MySQL 연동 - ODBC  (0) 2024.11.21
Boost 라이브러리 사용하기  (0) 2024.11.19
C++ MySQL 연동 - Connector/C++  (0) 2024.08.30
Google Protobuf - 4) 자동화 - 2  (0) 2024.08.21