加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

jsoncpp 备忘笔记

发布时间:2020-12-16 18:52:13 所属栏目:百科 来源:网络整理
导读:如题 int main() { 数组创建与分析: 例子一: string strValue = "{"ldh":"001","gfc":"002","yyj":"003","andy":["005","123","true"]}"; Json::Reader read; Json::Value value; value["ldh"] = "001"; value["gfc"] = "002"; value["a


如题

int main()  
{  
数组创建与分析:  
例子一:  
string strValue = "{"ldh":"001","gfc":"002","yyj":"003","andy":["005","123","true"]}";  
Json::Reader read;  
Json::Value value;  
value["ldh"] = "001";  
value["gfc"] = "002";  
value["andy"].append( "005" );  
value["andy"].append( "123" );  
value["andy"].append( "true" );  
//if( read.parse( strValue,value ) )  
{  
Json::Value val_array = value["andy"];  
int iSize = val_array.size();  
for ( int nIndex = 0;nIndex < iSize;++ nIndex )  
{  
cout<<val_array[nIndex]<<endl;  
}  
}  
  
  
例子二:  
Json::Reader read;  
Json::Value value;  
value["ldh"] = "001";  
value["gfc"] = "002";  
Value item;  
Value array;  
item["andy1"] = "005";  
array.append( item );  
item["andy1"] = "123";  
array.append( item );  
item["andy1"] = "true";  
array.append( item );  
value["andy"] = array;  
cout<<value.toStyledString()<<endl;  
Json::Value val_array = value["andy"];  
int iSize = val_array.size();  
for ( int nIndex = 0;nIndex < iSize;++ nIndex )  
{  
cout<<val_array[nIndex]<<endl;  
if ( !val_array[nIndex]["andy1"].isNull() )  
{  
cout<<val_array[nIndex]["andy1"]<<endl;  
}  
}  
  
  
例子三:  
std::string strValue = "{"name":"json","array":[{"cpp":"jsoncpp"},{"java":"jsoninjava"},{"php":"support"}]}";    
Json::Value value;  
Reader read;  
if ( !read.parse( strValue,value ) )  
{  
return -1;  
}  
cout<<value.toStyledString()<<endl;  
Json::Value val_array = value["array"];  
int iSize = val_array.size();  
for ( int nIndex = 0;nIndex < iSize;++ nIndex )  
{  
cout<<val_array[nIndex]<<endl;  
if ( val_array[nIndex].isMember( "cpp" ) )  
{  
cout<<val_array[nIndex]["cpp"]<<endl;  
}  
}  
  
getchar();  
return 0;  
}  

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读