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

  • 相关阅读:
    WEB前端工程师 – 职业生涯规划
    求Sn=a+aa+aaa+…+aaa…a的值
    输入一行字符,分别统计出其中英文字母 空格 数字和其他字符的个数
    getchar()的用法!
    求1+2+…+n的和不大于1000的最大自然数n
    编程打印输出*金字塔
    从键盘输入一个整数,判断该数是否回文数.
    编程求"水仙花数"
    编程求出1000以内的完全数
    输入两个正整数,求它们的最大公约数和最小公倍数.
  • 原文地址:https://www.cnblogs.com/fengbohello/p/4059435.html
Copyright © 2011-2022 走看看