首先下载这个软件,此次是libjson_7.5.0.zip。
1.makefile或手动的编译生成了两个lib文件libjsonDebug.lib和libjsonRelease.lib,放在当前目录下。
2.这个时候还不能直接用,需要修改文件JSONOptions.h。(行数可能有所波动)
(1).注释掉14行处的 #define JSON_LIBRARY
(2).注释掉317行处的 #define JSON_DEPRECATED_FUNCTIONS
(3).在31行处将#define JSON_DEBUG改成下面的代码:
#ifdef _DEBUG #define JSON_DEBUG #pragma comment(lib,"libjson/libjsonDebug.lib") #else #pragma comment(lib,"libjson/libjsonRelease.lib") #endif
#include "libjson/libjson.h" #include <iostream> void main() { JSONNode n(JSON_NODE); n.push_back(JSONNode("RootA", "Hello World")); JSONNode c(JSON_ARRAY); c.set_name("ArrayOfNumbers"); c.push_back(JSONNode("", 16)); c.push_back(JSONNode("", 42)); c.push_back(JSONNode("", 128)); n.push_back(c); std::string jc = n.write_formatted(); std::cout << jc << std::endl; system("pause"); }
4.来点复杂的,libjson的数组和对象读写,如下:(下面的代码release模式下没有问题,debug下会崩溃,原因待查)
////////////////////b.json file///////////////////////// { "RootA" : "Value in parent node", "ChildNode" : [ { "ChildA" : "String Value c1", "ChildB" : "dsf c1" }, { "ChildA" : "String Value c2", "ChildB" : "dsf c2" }, { "ChildA" : "String Value c3", "ChildB" : "dsf c3" } ] }
///////////////////////////////////////main.cpp///////////////////////////////////// #include "libjson/libjson.h" #include <stdio.h> #include <iostream> using namespace std; void main() { //write json JSONNode n(JSON_NODE); n.push_back(JSONNode("RootA", "Value in parent node")); JSONNode c(JSON_ARRAY); c.set_name("ChildNode"); JSONNode c1(JSON_NODE),c2(JSON_NODE); c1.push_back(JSONNode("ChildA","String Value c1")); c1.push_back(JSONNode("ChildB","dsf c1")); c2.push_back(JSONNode("ChildA","String Value c2")); c2.push_back(JSONNode("ChildB","dsf c2")); c.push_back(c1); c.push_back(c2); n.push_back(c); cout<<n.write_formatted()<<endl<<endl<<endl; //read json char* str=read_file("b.json"); if(libjson::is_valid(str)==false) { delete str; str=NULL; printf("Parse faild!\n"); system("pause"); exit(0); } JSONNode rn=libjson::parse(str); delete str; str=NULL; // ParseJSON(rn); for (int i=0;i<rn[1].size();i++) { JSONNode temp=rn[1][i]; for(int j=0;j<temp.size();j++) { cout<<temp[j].name()<<" : "<<temp[j].as_string()<<endl; } } ///////以下注释的方法是另外一种写法,可以留着 // JSONNode::const_iterator i = n[1].begin(); // while(i != n[1].end()) { // cout<<i->name()<<" : "<<i->as_string()<<endl; // i++; // } system("pause"); }
char* read_file(char *path) { char *json_; size_t length_; char *whitespace_; size_t whitespace_length_; FILE *fp = fopen(path, "rb"); if (!fp) fp = fopen(path, "rb"); fseek(fp, 0, SEEK_END); length_ = (size_t)ftell(fp); fseek(fp, 0, SEEK_SET); json_ = (char*)malloc(length_ + 1); fread(json_, 1, length_, fp); json_[length_] = '\0'; length_++; // include the null terminator fclose(fp); // whitespace test whitespace_length_ = 1024 * 1024; whitespace_ = (char *)malloc(whitespace_length_ + 4); char *p = whitespace_; for (size_t i = 0; i < whitespace_length_; i += 4) { *p++ = ' '; *p++ = '\n'; *p++ = '\r'; *p++ = '\t'; } *p++ = '['; *p++ = '0'; *p++ = ']'; *p++ = '\0'; // free(json_); free(whitespace_); return json_; }
void ParseJSON(const JSONNode & n) { JSONNode::const_iterator i = n.begin(); while (i != n.end()) { // recursively call ourselves to dig deeper into the tree if (i -> type() == JSON_ARRAY || i -> type() == JSON_NODE) { ParseJSON(*i); } if(i->name()!="" && i->as_string()!="")cout<<i->name()<<" : "<<i->as_string()<<endl; //increment the iterator ++i; } }
【转自:http://sss316.blog.163.com/blog/static/12893677620121724041866/】