[Jsoncpp] example : Basic, Read File / Write File (기본 사용법, 파일 읽고 쓰는 방법)

Posted by [하늘이]
2017. 1. 15. 15:11 IT/C, C++
반응형

jsoncpp download and compile 방법 : http://iam777.tistory.com/415




샘플 소스는 아래를 참고하세요.

Eclips Project로 생성된 Example Source.

JsonCppTest.tar.gz


사용법은 Jsoncpp 받은 경로에 Doc 폴더 안에 있는 문서를 참고하셔도 됩니다.

jsoncpp.dox



Basic --------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main() {
    cout << "!!!Hello World!!!" << endl// prints !!!Hello World!!!
 
    Json::Value root;
 
    root["encoding"= true;
    root["encoding2"= 1000;
    root["indent"]["length"= 10;
    root["indent"]["use_space"= -10;
    root["indent"]["length2"= 10.1;
    root["indent"]["use_space2"= "testString";
 
    cout << root << endl;
 
    bool boolvalue = root.get("encoding"false).asBool();
    cout << boolvalue << endl;
 
    int intvalue = root.get("encoding2"false).asInt();
    cout << intvalue << endl;
 
    int intSub1 = root["indent"].get("use_space"0).asInt();
    cout << "intSub1" << intSub1 << endl;
 
    //Cannot Get.....
    int intSub2 = root.get("use_space"10000).asInt();
    cout << "Cannot Get intSub2 : " << intSub2 <<"  Can get:"<< root["indent"]["use_space"]<< endl;
 
    char *path = (char*)JSON_FILE;
    testJsonReadFile(path);
    path = (char*)JSON_FILE_WRITE;
    testJsonWriteFile(path,root);
    return 0;
}
 
cs



Read File ---------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main() {
    cout << "!!!Hello World!!!" << endl// prints !!!Hello World!!!
 
    Json::Value root;
 
    root["encoding"= true;
    root["encoding2"= 1000;
    root["indent"]["length"= 10;
    root["indent"]["use_space"= -10;
    root["indent"]["length2"= 10.1;
    root["indent"]["use_space2"= "testString";
 
    cout << root << endl;
 
    bool boolvalue = root.get("encoding"false).asBool();
    cout << boolvalue << endl;
 
    int intvalue = root.get("encoding2"false).asInt();
    cout << intvalue << endl;
 
    int intSub1 = root["indent"].get("use_space"0).asInt();
    cout << "intSub1" << intSub1 << endl;
 
    //Cannot Get.....
    int intSub2 = root.get("use_space"10000).asInt();
    cout << "intSub2 : " << intSub2 <<"  "<< root["indent"]["use_space"]<< endl;
 
    char *path = (char*)JSON_FILE;
    testJsonReadFile(path);
    path = (char*)JSON_FILE_WRITE;
    testJsonWriteFile(path,root);
    return 0;
}
 
cs




Write File ---------------------

1
2
3
4
5
6
7
8
9
10
11
void testJsonWriteFile(char* path, Json::Value inputJsonRoot){
 
    Json::Value confRoot;
    Json::StyledStreamWriter wirter;
    std::ofstream writeOfStream(path);
 
    wirter.write(writeOfStream, inputJsonRoot);
 
    writeOfStream.close();
    cout << "testJsonWriteFile end:"<<  inputJsonRoot << endl;
}
cs


반응형

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

std::map 복사(std::copy)  (0) 2018.12.05
std::vector 복사(copy)  (0) 2018.12.05
[JSONCPP] JsonCpp 다운로드 및 컴파일 방법  (0) 2017.01.15
[C/C++] std::memcpy Api Reference  (0) 2016.10.21
[C/C++] std::vector::erase() Reference  (0) 2016.10.21