• 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

  • 相关阅读:
    threejs 通过bufferGeometry处理每一个点的位置和颜色
    nodejs通过buffer传递数据到前端,前端通过arraybuffer接收数据
    ubuntu 通过ssh上传/下载服务器文件
    前端通过浏览器导出文件并下载
    前端imageBuffer设置图片src(后端到前端直传buffer)
    一个FLAG #17# 测试STL
    一个FLAG #16# 优先队列
    一个FLAG #15# 团队队列
    一个FLAG #14# The SetStack Computer
    一个FLAG #13# V字型数列
  • 原文地址:https://www.cnblogs.com/mydomain/p/2241522.html
Copyright © 2020-2023  润新知