zoukankan      html  css  js  c++  java
  • [转]JsonCpp遍历json

    #include "json.h"  
    typedef Json::Writer JsonWriter;  
    typedef Json::Reader JsonReader;  
    typedef Json::Value  JsonValue;         
    void print(JsonValue v)  
    {      
        JsonValue::Members mem = v.getMemberNames();      
        for (auto iter = mem.begin(); iter != mem.end(); iter++)      
        {          
            cout<<*iter<<"	: ";          
            if (v[*iter].type() == Json::objectValue)          
            {              
                cout<<endl;              
                print(v[*iter]);          
            }          
            else if (v[*iter].type() == Json::arrayValue)          
            {              
                cout<<endl;              
                auto cnt = v[*iter].size();              
                for (auto i = 0; i < cnt; i++)              
                {                  
                    print(v[*iter][i]);              
                }          
            }          
            else if (v[*iter].type() == Json::stringValue)          
            {              
                cout<<v[*iter].asString()<<endl;          
            }          
            else if (v[*iter].type() == Json::realValue)          
            {              
                cout<<v[*iter].asDouble()<<endl;          
            }          
            else if (v[*iter].type() == Json::uintValue)          
            {              
                cout<<v[*iter].asUInt()<<endl;          
            }          
            else        
            {              
                cout<<v[*iter].asInt()<<endl;          
            }      
        }      
        return;  
    } 

    搜索json中指定的key的value

    //搜索json得到指定key的值,只支持stringvalue
    //如果strcontent不为空,使用strcontent解析,如果为空直接使用v
    string GetJsonStringValue(string strContent, string& strKey, Json::Value v)
    {
        if (!strContent.empty())
        {
            Json::Reader reader;
            if (!reader.parse(strContent, v))
            {
                return string("");
            }
        }
     
        Json::Value::Members mem = v.getMemberNames();
        Json::Value::Members::iterator it;
        //遍历所有key广度搜索
        for (it = mem.begin(); it != mem.end(); it++)
        {
            if (*it == strKey)
            {
                if (v[*it].type() == Json::stringValue)
                {
                    return v[*it].asString();
                }
                return string("");
            }
        }
     
        //如果未找到,再深度搜索
        for (it = mem.begin(); it != mem.end(); it++)
        {
            Json::ValueType type = v[*it].type();
            if (type == Json::objectValue)
            {
                string strvalue = GetJsonStringValue(string(""), strKey, v[*it]);
                if (!strvalue.empty())
                {
                    return strvalue;
                }
            }
            else if (type == Json::arrayValue)
            {
                for (int i = 0; i < v[*it].size(); i++)
                {
                    //如果在递归的过程中已找到,则返回
                    string strvalue = GetJsonStringValue(string(""), strKey, v[*it][i]);
                    if (!strvalue.empty())
                    {
                        return strvalue;
                    }
                }
            }
        }
     
        return string("");
    }
  • 相关阅读:
    [转]HD钱包的助记词与密钥生成原理
    [转]简单科普私钥、地址、助记词、Keystore的区别
    [转]Sequelize 中文API文档-4. 查询与原始查询
    [转]Node.JS使用Sequelize操作MySQL
    [转]OmniLayer / omnicore API 中文版
    [转]usdt omnicore testnet 测试网络
    [转]USDT与omniCore钱包
    [转]BTC RPC API GetTransaction
    [转]比特币测试链——Testnet介绍
    [转]BTC手续费计算,如何设置手续费
  • 原文地址:https://www.cnblogs.com/lyggqm/p/11888973.html
Copyright © 2011-2022 走看看