zoukankan      html  css  js  c++  java
  • 【JSONCpp】简介及demo

    一、JSON简介

    JSON

    一种轻量级的数据交换格式,易于阅读、编写、解析,全称为JavsScript ObjectNotation。

    JSON由两种基本结构组成

    ①   名字/值 对的集合,可以理解为对象

    ②   值的组合, 可以理解为数组

     

    示例

    string strTemp =
            "{ "name" :  "cuihao" ," 
            " "age" : 28 }";
    
        string strRoot = 
            "{ "key_string"  :  "value_string", " 
            "  "key_number"  :  28, "               
            "  "key_boolean" :  false,  "           
            "  "key_double"  :  12.345,  "          
            "  "key_object"  :  json_temp, "        
            "  "key_array"   :  ["array_string1",  "array_string2", 12345]}";

    二、JSONCPP

    1.     JsonCPP简介

         jsoncpp是c++解析JSON串常用的解析库之一。其常用的类有:

    a)     Json::Value    可以表示里所有的类型,比如int,string,object,array等,其支持的类型可以参考Json:ValueType中的值。

    b)     Json::Reader  将json文件流或字符串解析到Json::Value,主要函数有Parse。

    c)     Json::Writer   与Json::Reader相反,将Json::Value转化成字符串流,注意它的两个子类:Json::FastWriter和Json::StyleWriter,分别输出不带格式的json和带格式的json。

    d)     Json::Value::Members主要用于以STL风格解析JSON数组。看过源代码的人已知道,Members其实是typedefvector而已。

     

     

    在VC中使用JSONCPP

    下载源码后,进入D:SourceSoftwarejsoncpp-src-0.5.0jsoncpp-src-0.5.0makefilesvs71类似目录用VS打开.sln文件进行编译。

    注意:

    Debug下可以直接编译

    Release下:将lib_json项目属性:配置属性—C/C++--输出文件:汇编输出选择【无列表】,应为为No List。

    使用JSONCPP时,添加

    头文件目录:D:SourceSoftwarejsoncpp-src-0.5.0jsoncpp-src-0.5.0include

    库目录:

    Debug: D:SourceSoftwarejsoncpp-src-0.5.0jsoncpp-src-0.5.0uildvs71debuglib_json

    #pragma  commebt(lib, “json_vc71_libmtd.lib”) 对应/MDd

     

    Release: D:SourceSoftwarejsoncpp-src-0.5.0jsoncpp-src-0.5.0uildvs71 eleaselib_json

    #pragma  commebt(lib, “json_vc71_libmt.lib”)  对应/MT

    三、上代码

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include  <jsonjson.h> 
    using namespace std;
    
     
    int _tmain(int argc, _TCHAR* argv[])
    { 
    
        string strStudent = "{ "name" : "cuishihao", "age" : 28, "major" : "cs" }";
        //char* strStudent = "{ "name" : "cuishihao", "age" : 28, "major" : "cs" }";
    
        Json::Reader reader;
        Json::Value value;
        bool bRet = reader.parse(strStudent, value);
        if (false == bRet)
        {
            cerr << endl << "read value error 
    ";
            return -1;
        }
    
        cout << value["name"].asString()<<endl;
        cout << value["age"].asInt()<<endl;
        cout << value["major"].asString()<<endl;
    
        cout << endl;
    
        Json::Value json_temp;
        json_temp["name"] = Json::Value("cuihao");
        json_temp["age"] = Json::Value(28);
    
        Json::Value root;
        root["key_string"] = Json::Value("value_string");
        root["key_number"] = Json::Value(12345);
        root["key_boolean"] = Json::Value(true);
        root["key_double"] = Json::Value(12.345);
        root["key_object"] = json_temp;
        root["key_array"].append("array_string1");
        root["key_array"].append("array_string2");
        root["key_array"].append(12345);
        Json::ValueType type = root.type();
    
        Json::Value arrValue = root["key_array"];
        for (Json::Value::UInt i = 0; i < arrValue.size(); ++ i)
        {
            if (arrValue[i].isString()) 
                cout << arrValue[i].asString();
    
            else if (arrValue[i].isInt())
                cout << arrValue[i].asInt();
    
            cout << endl;
        }
    
        cout << endl << "----------------------------------- " <<endl;
    
    
        string strTemp =
            "{ "name" :  "cuihao" ," 
            " "age" : 28 }";
    
        string strRoot = 
            "{ "key_string"  :  "value_string", " 
            "  "key_number"  :  28, "               
            "  "key_boolean" :  false,  "           
            "  "key_double"  :  12.345,  "          
            "  "key_object"  :  json_temp, "        
            "  "key_array"   :  ["array_string1",  "array_string2", 12345]}";
    
        Json::StyledWriter writer;
        cout << endl << writer.write(root) << endl;
    
        cout << endl << "----------------------------"<<endl;
     
         
        Json::Value::Members members = root.getMemberNames();
    
        for(Json::Value::Members::const_iterator iter = members.begin();
            iter != members.end();
            ++ iter)
        {
            string strName = *iter; 
           
            if (root[strName].isInt())        
                cout << root[strName].asInt();
            
            else if (root[strName].isString()) 
                cout << root[strName].asString();
    
            else if (root[strName].isDouble()) 
                cout << root[strName].asDouble();
    
            else if(root[strName].isBool())
                cout << root[strName].asBool();
    
            else if(root[strName].isArray())
            {
                for(Json::Value::ArrayIndex i = 0; i < root[strName].size(); ++ i)
                { 
                    if(root[strName][i].isInt())
                        cout << root[strName][i].asInt();
                    else if(root[strName][i].isString())
                        cout << root[strName][i].asString();
                    else 
                        cout << "others";
    
                    cout << endl;
                }
            }
     
            else if (root[strName].isObject())
            {
                Json::Value::Members mbs = root[strName].getMemberNames();
                for (Json::Value::Members::const_iterator iter2 = mbs.begin();
                    iter2 != mbs.end();
                    ++ iter2)
                {
                    string strName2 = *iter2;
                    if(root[strName][strName2].isInt())
                        cout << root[strName][strName2].asInt();
                    else if(root[strName][strName2].isString())
                        cout << root[strName][strName2].asString();
                    else
                        cout << "others";
    
                    cout << endl;  
                } //for
            } //else if
            else
                cout << "others";
    
            cout << endl;
        }
    
        return 0;
    }

    执行结果

      

  • 相关阅读:
    MySql不同版本对表名大小写敏感!
    jQuery Intellisense in VS 2008
    控制台方式运行java程序示例
    有关SQL Server 2008安装时“安全程序支持规则”失败的解决方法
    正则表达式实现验证的技术总结
    网站开发架构设计要求
    jquery select控件的相关操作
    Windows Embedded Standard 7 开发随笔
    一步步做自己的webinstall安装包
    仿新浪新闻中异步替换关键字
  • 原文地址:https://www.cnblogs.com/cuish/p/3888657.html
Copyright © 2011-2022 走看看