zoukankan      html  css  js  c++  java
  • jsoncpp操作 json

    Cpp代码  收藏代码
    1. #include <iostream>  
    2.   
    3. //#include "json/json.h"  
    4.   
    5. #include "json_reader.cpp"  
    6. #include "json_value.cpp"  
    7. #include "json_writer.cpp"  
    8.   
    9. using namespace std;  
    10. using namespace Json;  
    11.   
    12. int main(int argc, char *argv[])  
    13. {  
    14.       
    15.     // Configuration options  
    16.     char *config_doc=" { "encoding" :"UTF-8","plug-ins" : ["python","c++","ruby"],"indent" : { "length" : 3, "use_space": true }}";  
    17.   
    18.     Json::Value root;   // will contains the root value after parsing.  
    19.     Json::Reader reader;  
    20.     bool parsingSuccessful = reader.parse( config_doc, root );  
    21.     if ( !parsingSuccessful )  
    22.     {  
    23.         // report to the user the failure and their locations in the document.  
    24.         std::cout  << "Failed to parse configuration "  
    25.                    << reader.getFormattedErrorMessages();  
    26.         return 1;  
    27.     }  
    28.   
    29.     reader.parse(config_doc,root,false);     //解析出json放到json中  
    30.       
    31.     std::string encoding = root.get("encoding""GBK" ).asString();  
    32.     const Json::Value plugins = root["plug-ins"];  
    33.       
    34.     for(int index = 0; index < plugins.size(); ++index ){  
    35.             cout<<plugins[index];  
    36.     }  
    37.       
    38.     cout<< plugins;  
    39.     cout<<encoding<<endl;  
    40.       
    41.     root["encoding"] = "GB2312";  
    42.     root["indent"]["length"] =  5;  
    43.     root["indent"]["use_space"] = false;  
    44.                    
    45.     Json::StyledWriter writer;  
    46.       
    47.     std::string outputConfig = writer.write( root );   
    48.       
    49.     cout<<outputConfig;  
    50.                                       
    51.     system("PAUSE");  
    52.     return 0;  
    53. }  
     
  • 相关阅读:
    Flex 布局教程:语法篇(转载)
    【Go】【Http】Go实现Http相关知识点
    【Git】Git相关开发流程
    【Go】杂七杂八GoLang
    【Go】初识Context与Context键值对的可能情况
    jmeter-通过json提取器 提取所有数据 给下个接口使用
    C# 后台调用存储过程超时处理方法,
    IE11脚本错误-调用的对象无效-
    IE11浏览器arrt,全选反选失效无效修改方法
    如何学习计算机知识
  • 原文地址:https://www.cnblogs.com/MingZznet/p/3228179.html
Copyright © 2011-2022 走看看