随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

c/c++ jsoncpp的基本使用

一、概述

  jsoncpp官网

  作用:在c++中可以方便的组装及解析json格式的数据。

二、代码示例

复制代码
void MyJsonCpp::toJsonStr() {
    Json::Value jsonValue;
    jsonValue["username"] = "luoluoyang";
    jsonValue["password"] = "123456";
    jsonValue["age"] = 6;
    jsonValue["sex"] = "man";
    cout << "toJsonStr=" << jsonValue.toStyledString().c_str() << endl;
}

void MyJsonCpp::parseJson() {
    //字符串
    string str = "{\"name\":\"shuiyixin\",\"age\":21,\"sex\":\"man\"}";
    //声明类的对象
    Json::Reader reader;
    Json::Value root;
    //从字符串读取数据
    if (reader.parse(str, root)) {
        string name = root["name"].asString();
        int age = root["age"].asInt();
        string sex = root["sex"].asString();
        cout << root.toStyledString() << endl;
    }
}

void MyJsonCpp::writeFileJson() {
    //根节点
    Json::Value root;
    root["name"] = Json::Value("LOL");
    root["age"] = Json::Value("18");
    root["sex"] = Json::Value("male");
    //子节点
    Json::Value bro;
    bro["friend_name"] = "德玛西亚";
    bro["friend_age"] = Json::Value("18");
    bro["friend_sex"] = "";
    root["friend"] = Json::Value(bro);
    cout << root.toStyledString() << endl;

}
复制代码

 

posted on   飘杨......  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2021-07-23 CentOS系统编译coturn时遇到的错误
2015-07-23 android ActionBarSherlock使用说明
2013-07-23 Android 一s个相对完整的自动升级功能实现代码
2013-07-23 Android 一个相对完整的自动升级功能实现代码
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示