jsoncpp解析拼装数组

Cocos2d-x添加jsoncpp应该资料都有了,今天来讲讲数组的解析和拼装~

[cpp] view plain copy
 
 
 
  1. int main()  
  2. {  
  3. 数组创建与分析:  
  4. 例子一:  
  5. string strValue = "{"ldh":"001","gfc":"002","yyj":"003","andy":["005","123","true"]}";  
  6. Json::Reader read;  
  7. Json::Value value;  
  8. value["ldh"] = "001";  
  9. value["gfc"] = "002";  
  10. value["andy"].append( "005" );  
  11. value["andy"].append( "123" );  
  12. value["andy"].append( "true" );  
  13. //if( read.parse( strValue,value ) )  
  14. {  
  15. Json::Value val_array = value["andy"];  
  16. int iSize = val_array.size();  
  17. for ( int nIndex = 0;nIndex < iSize;++ nIndex )  
  18. {  
  19. cout<<val_array[nIndex]<<endl;  
  20. }  
  21. }  
  22.   
  23.   
  24. 例子二:  
  25. Json::Reader read;  
  26. Json::Value value;  
  27. value["ldh"] = "001";  
  28. value["gfc"] = "002";  
  29. Value item;  
  30. Value array;  
  31. item["andy1"] = "005";  
  32. array.append( item );  
  33. item["andy1"] = "123";  
  34. array.append( item );  
  35. item["andy1"] = "true";  
  36. array.append( item );  
  37. value["andy"] = array;  
  38. cout<<value.toStyledString()<<endl;  
  39. Json::Value val_array = value["andy"];  
  40. int iSize = val_array.size();  
  41. for ( int nIndex = 0;nIndex < iSize;++ nIndex )  
  42. {  
  43. cout<<val_array[nIndex]<<endl;  
  44. if ( !val_array[nIndex]["andy1"].isNull() )  
  45. {  
  46. cout<<val_array[nIndex]["andy1"]<<endl;  
  47. }  
  48. }  
  49.   
  50.   
  51. 例子三:  
  52. std::string strValue = "{"name":"json","array":[{"cpp":"jsoncpp"},{"java":"jsoninjava"},{"php":"support"}]}";    
  53. Json::Value value;  
  54. Reader read;  
  55. if ( !read.parse( strValue,value ) )  
  56. {  
  57. return -1;  
  58. }  
  59. cout<<value.toStyledString()<<endl;  
  60. Json::Value val_array = value["array"];  
  61. int iSize = val_array.size();  
  62. for ( int nIndex = 0;nIndex < iSize;++ nIndex )  
  63. {  
  64. cout<<val_array[nIndex]<<endl;  
  65. if ( val_array[nIndex].isMember( "cpp" ) )  
  66. {  
  67. cout<<val_array[nIndex]["cpp"]<<endl;  
  68. }  
  69. }  
  70.   
  71. getchar();  
  72. return 0;  
  73. }  
原文地址:https://www.cnblogs.com/lidabo/p/9436189.html