Mingz技术博客

...

导航

C++之jsoncpp学习

最新由于客户端要用到jsoncpp,所以自己也跟着项目的需求学了一下jsoncpp.以前没用过xml,但是感觉接触json后,还蛮好用的。

参考地址 http://jsoncpp.sourceforge.net/class_json_1_1_value.html

 

#include <QtCore/QCoreApplication>

#include <json/json.h>

#include <string>

using std::string;

int main(int argc, char *argv[])

{

   Json::Reader reader;      

   Json::Value json_object;

    const char* json_document = "{/"age/" : 26,/"person/":[{/"id/":1,/"study/":[{/"language/":/"chinese/"}],/"name/":/"chen/"},{/"id/":2,/"name/":/"zhang/"}],/"name/" : /"huchao/"}";

    if (!reader.parse(json_document, json_object))//读入字符串,然后转换成json格式

    return 0;

 

   int a1=0;

    json_object["name"] ="wang";//修改数据

   json_object["person"][a1]["study"][a1]["language"] = "English";

 

    std::cout << json_object["name"] << std::endl;//读取数据

    std::cout << json_object["person"][a1]["study"][a1]["language"]<< std::endl;

    std::cout << json_object["age"] << std::endl;

 

    Json::Value json_object;

    json_object.append("11");   //加入数组格式

    json_object.append("12");

    json_object.append("13");

 

    return a.exec();

}

 

要下载jsoncpp,然后程序包含jsoncpp源码。

 

posted on 2013-07-31 16:43  Mingz2013  阅读(255)  评论(0编辑  收藏  举报