zoukankan      html  css  js  c++  java
  • 5、JsonCpp简单使用(1)

    1、反序列化Json对象示例

    示例代码

    View Code
    #include <iostream>
    #include <string>
    #include "json/json.h"

    int main(void)
    {
    std::string strValue = "{\"key1\":\"value1\",\"array\":[{\"key2\":\"value2\"},{\"key2\":\"value3\"},{\"key2\":\"value4\"}]}";
    Json::Reader reader;
    Json::Value value;

    if (reader.parse(strValue, value))
    {
    std::string out = value["key1"].asString();
    std::cout << out << std::endl;
    const Json::Value arrayObj = value["array"];
    for (int i = 0; i < arrayObj.size(); i++)
    {
    out = arrayObj[i]["key2"].asString();
    std::cout << out;
    }
    }
    return 0;
    }

    2、序列化对象

    示例代码

    View Code
    #include <iostream>
    #include <string>
    #include "json/json.h"

    int main(void)
    {
    Json::Value root;
    Json::Value arrayObj;
    Json::Value item;

    for (int i = 0; i < 10; i ++)
    {
    item["key"] = i;
    arrayObj.append(item);
    }

    root["key1"] = "value1";
    root["key2"] = "value2";
    root["array"] = arrayObj;
    //root.toStyledString();
    std::string out = root.toStyledString();
    std::cout << out << std::endl;
    return 0;
    }

     

    result
    {
    "array" : [
    {
    "key" : 0
    },
    {
    "key" : 1
    },
    {
    "key" : 2
    },
    {
    "key" : 3
    },
    {
    "key" : 4
    },
    {
    "key" : 5
    },
    {
    "key" : 6
    },
    {
    "key" : 7
    },
    {
    "key" : 8
    },
    {
    "key" : 9
    }
    ],
    "key1" : "value1",
    "key2" : "value2"
    }

    编译:

    g++ -o json_t json_t2.cpp -I/usr/include/json /usr/include/json/libjson.a son.a

    3、示例3

    另一种方法来遍历数据

    示例代码

    View Code
    #include <iostream>
    #include <string>
    #include "json/json.h"

    using namespace std;
    int main(void)
    {
    string strValue = "{\"key1\":\"value1\",\"array\":[{\"key2\":\"value2\",\"key3\":\"aa\"},{\"key2\":\"value3\",\"key3\":\"bb\"},{\"key2\":\"value4\",\"key3\":\"cc\"}]}";
    Json::Reader reader;
    Json::Value value;

    //method1
    if (reader.parse(strValue, value))
    {
    std::string out = value["key1"].asString();
    std::string out2;
    std::cout << out << std::endl;

    const Json::Value arrayObj = value["array"];
    for (int i = 0; i < arrayObj.size(); i++)
    {
    out = arrayObj[i]["key2"].asString();
    out2 = arrayObj[i]["key3"].asString();
    std::cout << out << " : " << out2 << endl;
    }
    }
    cout << "----------------" << endl;

    //另一种,用他内建的迭代器,其实也就是他自己的一个vector<string>成员,
    //可以自己去看json:value的定义
    Json::Value::Members member;//Members 就是vector<string>,typedef了而已
    if (reader.parse(strValue, value))
    {
    std::string out = value["key1"].asString();
    std::string out2;
    std::cout << out << std::endl;

    const Json::Value arrayObj = value["array"];
    for (Json::Value::iterator itr = arrayObj.begin(); itr != arrayObj.end(); itr++)
    {
    member = (*itr).getMemberNames();
    string out1, out2;
    bool flag = true;
    for (Json::Value::Members::iterator iter = member.begin(); iter != member.end(); iter++)
    {
    if (flag)
    {
    out1 = (*itr)[(*iter)].asString();
    flag = false;
    }
    else
    {
    out2 = (*itr)[(*iter)].asString();
    }
    }
    cout << out1 << " : " << out2 << endl;
    }
    }
    return 0;
    }

     

    View Code
    value1
    value2 : aa
    value3 : bb
    value4 : cc
    ----------------
    value1
    value2 : aa
    value3 : bb
    value4 : cc

    参考

    1】 示例来源网址

    http://www.cnblogs.com/logicbaby/archive/2011/07/03/2096794.html

  • 相关阅读:
    如何添加mysql到环境变量
    SQLyog客户端无法连接MySQL服务器
    linux下插入的mysql数据乱码问题及第三方工具显示乱码问题
    mysql-5.7.10产生的日志时间与系统时间不一致
    linux—文件目录简单介绍
    python编程中的if __name__ == 'main' 的作用和原理
    Windows下Python版本的切换
    python—第三库的安装方法
    阿里云ubuntu16.04安装beef
    xss利用-beef攻击-演示
  • 原文地址:https://www.cnblogs.com/mydomain/p/2241522.html
Copyright © 2011-2022 走看看