logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

Rapid JSON 간단 사용법

by digipine posted Aug 27, 2024
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

JSON 문자열을 객체로 변환하는 예제

  1. 라이브러리 포함: 먼저, rapidjson 헤더 파일을 포함해야 합니다.

    #include <rapidjson/document.h>
    #include <rapidjson/writer.h>
    #include <rapidjson/stringbuffer.h>
    
    
  2. JSON 문자열 정의: 변환할 JSON 문자열을 정의합니다.

    const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
    
  3. 문서 객체 생성 및 파싱: rapidjson::Document 객체를 생성하고 Parse 메소드를 사용하여 JSON 문자열을 파싱합니다.

    rapidjson::Document document;
    if (document.Parse(json).HasParseError()) {
        // 파싱 에러 처리
        std::cerr << "JSON parse error: " << document.GetParseError() << std::endl;
        return -1;
    }
    
  4. 데이터 접근: 파싱한 JSON 데이터를 객체처럼 접근할 수 있습니다.

    if (document.HasMember("project") && document["project"].IsString()) {
        std::cout << "Project: " << document["project"].GetString() << std::endl;
    }
    
    if (document.HasMember("stars") && document["stars"].IsInt()) {
        std::cout << "Stars: " << document["stars"].GetInt() << std::endl;
    }
    

전체 예제 코드

아래는 위의 절차를 모두 포함한 전체 예제 코드입니다.

 

이 예제 코드를 컴파일하고 실행하면 Project: rapidjsonStars: 10이 출력됩니다.

#include <iostream>
#include <rapidjson/document.h>

int main() {
    // JSON 문자열 정의
    const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";

    // Document 객체 생성
    rapidjson::Document document;

    // JSON 문자열 파싱
    if (document.Parse(json).HasParseError()) {
        // 파싱 에러 처리
        std::cerr << "JSON parse error: " << document.GetParseError() << std::endl;
        return -1;
    }

    // 데이터 접근 및 출력
    if (document.HasMember("project") && document["project"].IsString()) {
        std::cout << "Project: " << document["project"].GetString() << std::endl;
    }

    if (document.HasMember("stars") && document["stars"].IsInt()) {
        std::cout << "Stars: " << document["stars"].GetInt() << std::endl;
    }

    return 0;
}

rapidjson은 매우 강력하고 유연한 라이브러리이므로, 다양한 JSON 데이터 구조를 처리할 수 있습니다. 더 복잡한 JSON 데이터를 처리하려면 Document 객체의 여러 메소드와 속성을 활용하면 됩니다.

TAG •

  1. No Image 20Jan
    by lizard2019
    2025/01/20 by lizard2019
    Views 84 

    rmix test application 배포

  2. No Image 05Nov
    by lizard2019
    2024/11/05 by lizard2019
    Views 538 

    윈도우에서 패스워드 입력 실패로 잠금 상태인지 확인 하는 방법

  3. No Image 30Aug
    by digipine
    2024/08/30 by digipine
    Views 502 

    OpenSSL Build for Windows

  4. No Image 27Aug
    by digipine
    2024/08/27 by digipine
    Views 991 

    Rapid JSON 간단 사용법

  5. No Image 27Aug
    by digipine
    2024/08/27 by digipine
    Views 782 

    Direct X 11에서 그래픽 카드의 정보 가져오는 예제

  6. No Image 27Aug
    by digipine
    2024/08/27 by digipine
    Views 805 

    Python Slack 메시지 발송하는 예제

  7. No Image 27Aug
    by digipine
    2024/08/27 by digipine
    Views 614 

    Python email 보내는 예제 코드

  8. No Image 08Aug
    by digipine
    2024/08/08 by digipine
    Views 501 

    UDP 핀홀 트래버설 과정 요약, UDP pinhole traversal

  9. No Image 08Aug
    by digipine
    2024/08/08 by digipine
    Views 515 

    NAT 상태에서 P2P 통신하는 방법

  10. Windows Visual Studio 2022 OpenSSL Build 방법

  11. No Image 28Mar
    by digipine
    2024/03/28 by digipine
    Views 732 

    Visual Studio 단축키 정리

  12. 프로그래밍 언어 순위 2023년

  13. No Image 11Aug
    by digipine
    2023/08/11 by digipine
    Views 839 

    이벤트 텍소노미(Event Taxonomy)란 무엇인가요?

  14. Bitbucket에서 SSH 키 등록하고 사용하는 방법 (맥/리눅스)

  15. No Image 12May
    by digipine
    2023/05/12 by digipine
    Views 852 

    FFServer RTSP Audio Server Config

  16. No Image 15Feb
    by lizard2019
    2023/02/15 by lizard2019
    Views 867 

    OBS Studio for Http Interface EXE

  17. No Image 06Oct
    by digipine
    2022/10/06 by digipine
    Views 1496 

    xcode xib encountered an error communicating with ibagent-ios 해결

  18. No Image 25Sep
    by lizard2019
    2022/09/25 by lizard2019
    Views 1357 

    XCode 사용시 git ignore 로 xcuserstate 충돌 해결하기, .gitignore에 등록했는데도 동작안할때 해결방법

  19. No Image 05Sep
    by digipine
    2022/09/05 by digipine
    Views 1408 

    MAC Screen Sharing을 위한 VNC 접속을 위한 Port 변경 방법

  20. No Image 26Jan
    by digipine
    2022/01/26 by digipine
    Views 1679 

    Phabricator Ubuntu Installation Guide

Board Pagination Prev 1 2 3 4 5 6 Next
/ 6

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5