libjson 編譯和使用 - 2. 配置使用lib文件


以下轉自:http://blog.csdn.net/laogong5i0/article/details/8223448

1. 在之前的libjson所在的解決方案里新建一個控制台應用程序,叫TestLibjson。

2. 右鍵TestLibjson項目,選擇屬性。按下圖設置導入libjson的頭文件。(雖然我們已經編譯成lib庫文件,但我們還是要在我們的項目里加入頭文件。)

3. 在屬性里加入剛剛生產的libjson.lib文件。如下圖設置。

好了,配置設置好了,接下來我們寫寫測試代碼

首先新建下列文.h和.cpp文件

在TestLibjson.h文件加入以下代碼。

 
1 #include "libjson.h"  
2 class TestLibjson  
3 {  
4 public:  
5     TestLibjson();  
6     void ParseJSON(JSONNODE *n);  
7 };  

 


在TestLibjson.cpp文件加入代碼。

 
 1 // TestLibjson.cpp : 定義控制台應用程序的入口點。  
 2 //  
 3   
 4 #include "stdafx.h"  
 5 #include <stdlib.h>  
 6 #include "TestLibjson.h"  
 7 #include "libjson.h"  
 8   
 9 TestLibjson::TestLibjson()//構造函數  
10 {  
11 }  
12   
13 void TestLibjson::ParseJSON(JSONNODE *n){//解析json文件  
14     if (n == NULL){  
15         printf("Invalid JSON Node\n");  
16         return;  
17     }  
18    
19     JSONNODE_ITERATOR i = json_begin(n);  
20     while (i != json_end(n)){  
21         if (*i == NULL){  
22             printf("Invalid JSON Node\n");  
23             return;  
24         }  
25    
26         // recursively call ourselves to dig deeper into the tree  
27         if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE){  
28             ParseJSON(*i);  
29         }  
30    
31         // get the node name and value as a string  
32         json_char *node_name = json_name(*i);  
33    
34         // find out where to store the values  
35         if (strcmp(node_name, "RootA") == 0){  
36             json_char *node_value = json_as_string(*i);  
37             printf("rootA: %s\n", node_value);  
38             json_free(node_value);  
39         }  
40         else if (strcmp(node_name, "ChildA") == 0){  
41             json_char *node_value = json_as_string(*i);  
42             printf("ChildA: %s\n", node_value);  
43             json_free(node_value);  
44         }  
45         else if (strcmp(node_name, "ChildB") == 0)  
46             printf("childB: %d\n", json_as_int(*i));  
47         // cleanup and increment the iterator  
48         json_free(node_name);  
49         ++i;  
50     }  
51     system("pause");  
52 }  
53   
54 int _tmain()//程序入口  
55 {  
56     char *json = "{\"RootA\":\"Value in parent node\",\"ChildNode\":{\"ChildA\":\"String Value\",\"ChildB\":42}}";  
57     JSONNODE *n = json_parse(json);  
58     TestLibjson tl = TestLibjson();  
59     tl.ParseJSON(n);  
60     json_delete(n);  
61   
62     return 0;  
63 }  

 

運行結果

 

 

注意,這里我們用的的libjson的Debug模式,如果你用的是release模式,那你還需要設置libOption.h文件,把它的#define JSON_DEBUG 注釋掉。

 

下一篇:libjson 編譯和使用 - 3. libjson的C接口 API


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM