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("");
    }
  • 相关阅读:
    新增更改app.Config的值
    repeater DropDownList 事件
    ASP.NET 状态服务 及 session丢失问题解决方案总结
    js动态添加table的行
    各大社交网络首页黄金区输入框提示(facebook,人人网,开心网)
    Color theme installation for Emacs in Windows 7
    乐观锁和悲观锁
    google的落寞
    印象深刻的网络实验课
    未知和恐惧
  • 原文地址:https://www.cnblogs.com/lyggqm/p/11888973.html
Copyright © 2011-2022 走看看