zoukankan      html  css  js  c++  java
  • C++ 解析json串

    首先, C++ 解析json串,需要用到第三方库(json_vc71_libmtd.lib)。然后,VS2010,创建项目json_read, 配置项目属性。最后,拷贝下面的代码就可以看到效果了。

    #include "stdafx.h"
    #include "../json/include/json.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        const char * str = "{"machineCode":"20:20:20:20:20:20:57:4c:31:30:59:31:4d:56","makeTime":1534485879,"sysCapacity":{"rptMaxNum":2},"trialTime":30}";
        printf("json 串:%s
    ", str);
    
        Json::Reader reader;
        Json::Value  root;
        if(reader.parse(str,root))
        {
            std::string machineCode = root["machineCode"].asString();
            long long makeTime  = root["makeTime"].asUInt();
            int rptMaxNum = root["rptMaxNum"].asInt();
            int trialTime = root["trialTime"].asInt();
    
            printf("解析json串之后的值:
    ");
            printf("machineCode = %s
    ",machineCode.c_str());
            printf("makeTime = %ld
    ",makeTime);
            printf("rptMaxNum = %d
    ",rptMaxNum);
            printf("trialTIme = %d
    ",trialTime);
    
            Json::Value  & sysCapacity = root["sysCapacity"];
            int rpt = sysCapacity["rptMaxNum"].asInt();
            printf("rptMaxNum = %d
    ",rpt);
        }
        system("pause");
        return 0;
    }

    附I:json在线格式化工具

    附II:项目用到第三方库资源,有库,有头文件的时候,建议分类创建一个文件夹,便于阅读和重用。

    附录III:运行结果

  • 相关阅读:
    实验二
    2
    DS博客作业08--课程总结
    DS博客作业07--查找
    DS博客作业06--图
    DS博客园作业05--树
    有向图强连通分量Tarjan算法
    nyoj 题目737 合并石子(一)
    nyoj 题目61 传纸条
    nyoj 题目49 开心的小明
  • 原文地址:https://www.cnblogs.com/azbane/p/10175664.html
Copyright © 2011-2022 走看看