zoukankan      html  css  js  c++  java
  • jsoncpp代码实例

    最近开始使用 jsoncpp,以前一直在使用cJSON,但是使用cJSON的时候经常会忘记free掉json的内存,结果造成了内存泄露,程序跑着跑着就崩溃了。所以最近把json转移到了jsoncpp上。

    jsoncpp的代码源文件 百度网盘 : http://pan.baidu.com/s/1ntqQhIT

    下面是jsoncpp源代码中的main.cpp实例,大家感受一下。

    jsoncpp代码包解压后的目录结构如下:

    jsoncpp/
    ├─ include
    │   └─ json
    │       ├── autolink.h
    │       ├── config.h
    │       ├── features.h
    │       ├── forwards.h
    │       ├── json.h
    │       ├── reader.h
    │       ├── value.h
    │       └── writer.h
    ├─ makefile
    └─ src
        ├─ json
        │   ├── json_batchallocator.h
        │   ├── json_internalarray.inl
        │   ├── json_internalmap.inl
        │   ├── json_reader.cpp
        │   ├── json_value.cpp
        │   ├── json_valueiterator.inl
        │   ├── json_writer.cpp
        │   └── sconscript
        └── main.cpp

    其中jsoncpp/src/main.cpp就是我们的测试程序代码。

    在jsoncpp目录下直接运行命令make即可进行编译(linux环境下)。

     1 #include <string>
     2 #include <json/json.h>
     3 
     4 void readJson();
     5 void writeJson();
     6 
     7 int main(int argc, char** argv) {
     8     readJson();
     9     writeJson();
    10     return 0;
    11 }
    12 
    13 void readJson() {
    14     using namespace std;
    15     std::string strValue = "{"name":"json","array":[{"cpp":"jsoncpp"},{"java":"jsoninjava"},{"php":"support"}]}";
    16 
    17     Json::Reader reader;
    18     Json::Value value;
    19 
    20     if (reader.parse(strValue, value))
    21     {
    22         std::string out = value["name"].asString();
    23         std::cout << out << std::endl;
    24         const Json::Value arrayObj = value["array"];
    25         for (unsigned int i = 0; i < arrayObj.size(); i++)
    26         {
    27             if (!arrayObj[i].isMember("cpp")) 
    28                 continue;
    29             out = arrayObj[i]["cpp"].asString();
    30             std::cout << out;
    31             if (i != (arrayObj.size() - 1))
    32                 std::cout << std::endl;
    33         }
    34     }
    35 }
    36 
    37 void writeJson() {
    38     using namespace std;
    39 
    40     Json::Value root;
    41     Json::Value arrayObj;
    42     Json::Value item;
    43 
    44     item["cpp"] = "jsoncpp";
    45     item["java"] = "jsoninjava";
    46     item["php"] = "support";
    47     arrayObj.append(item);
    48 
    49     root["name"] = "json";
    50     root["array"] = arrayObj;
    51 
    52     root.toStyledString();
    53     std::string out = root.toStyledString();
    54     std::cout << out << std::endl;
    55 }

    下面是运行的结果

     1 json
     2 jsoncpp
     3 {
     4    "array" : [
     5       {
     6          "cpp" : "jsoncpp",
     7          "java" : "jsoninjava",
     8          "php" : "support"
     9       }
    10    ],
    11    "name" : "json"
    12 }

    作者:风波

    mail : fengbohello@qq.com

  • 相关阅读:
    k8s是什么
    jmeter性能测试的策略
    面试的灵魂拷问:你最大的缺点是什么?
    面试的灵魂拷问:你做过的最令你自豪的事情是什么?
    面试的灵魂拷问:你犯过的最大的失误是什么?
    5-django rest framework,搭建api,这是最重要的章节
    4-restfulapi的介绍,vue代码结构
    组合恒等式证明——「Zeilberger 老爷子的 T 恤上写了啥?」
    「NOI2020」 美食家 【矩阵快速幂】
    [CEOI2014] The Wall【最短路】
  • 原文地址:https://www.cnblogs.com/fengbohello/p/4059435.html
Copyright © 2011-2022 走看看