使用JsonCpp解析json格式的字符

分类: C++ 发布时间: 2017-11-05 20:42

最近需要解析json格式的通讯协议,自己从json官网上下载json的库,并且编译生成dll库,添加了测试程序。稍微处理了一点的是 : 判断json中是否存在某一个字段,使用get方法。下面介绍一个简单的解析示例,详细的编译程序查看以下地址:

链接:http://pan.baidu.com/s/1c2Nl3w 密码:dsda

void ParseJsonString()
{
std::string strValue = "{\"chnstatistics\":[\
{\"chn\":\"0\",\"hasaudio\":\"1\",\"resolution\":\"960H\",\"framerate\":\"25\",\"bitrate\":\"978\", \"needspace\":\"0.419\",\"resolutionlist\":\"1,2,3,4\",\"picquality\":\"4\",\"H264+\":\"1\",\"copyto\":\"1,2,3,4\" },\
{\"chn\":\"1\",\"hasaudio\":\"0\",\"resolution\":\"720P\",\"framerate\":\"25\",\"bitrate\":\"1431\", \"needspace\":\"0.613\",\"resolutionlist\":\"1,2,3,4\",\"picquality\":\"4\",\"H264+\":\"1\",\"copyto\":\"1,2,3,4\" }\
],\"basebitrate\":[\
{ \"resolution1\":\"763, 902, 1041, 1180, 1319,1457\",\"resolution2\":\"1457, 1596, 1735, 1874, 2013,2152\", \"resolution3\":\"1457, 1596, 1735, 1874, 2013,2152\" }\
],\"frameratelist\":\"1,3,8,11,15,20,25\",\"streamtype\":\"0\"}";

Json::Reader reader;
Json::Value value;
bool ok= reader.parse( strValue, value);

Json::Value streamtype = value[“streamtype”];
std::string streamtypevalue = “0”;
if ( !streamtype.empty() )
{
streamtypevalue = streamtype.asString();
}

Json::Value statistics = value[“chnstatistics”];

int isize = statistics.size();
for ( int i=0; i<isize; i++ )
{
Json::Value valuechn = statistics[i];

int valuechnsize = valuechn.size();

Json::Value isexistValue = valuechn.get(“hasaudio”, “null” );
if( isexistValue != “null” )
{
MessageBox( NULL, L”hasaudio”, L”Msg”, 0 );
}

Json::Value isexisttestValue = valuechn.get(“test”, “null” );
if( isexisttestValue != “null” )
{
MessageBox( NULL, L”hasaudio”, L”Msg”, 0 );
}
else
{
MessageBox( NULL, L”not has test”, L”Msg”, 0 );
}

std::string chnnum = valuechn[“chn”].asString();
std::string hasaudio = valuechn[“hasaudio”].asString();
std::string resolution = valuechn[“resolution”].asString();
std::string framerate = valuechn[“framerate”].asString();
std::string bitrate = valuechn[“bitrate”].asString();
std::string needspace = valuechn[“needspace”].asString();
}

Json::Value framelist = value[“frameratelist”];
std::string framelistvalue = framelist.asString();

}

扫描下方二维码,关注业余草微信公众号,回复“FFmpeg”关键词,获取 FFmpeg 视频教程!

关注公众号获取视频教程

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!