JSON

C++处理JSON数据格式

字号+ 作者:H5之家 来源:H5之家 2015-09-20 17:33 我要评论( )

一、摘要JSON 的全称为:JavaScript Object Notation,顾名思义,JSON 是用于标记 Javascript 对象的,JSON 官方的解释为:JSON 是一种轻量级的数据传输格式。本

Json::Value root;   // will contains the root value after parsing.

Json::Reader reader;

bool parsingSuccessful = reader.parse( config_doc, root );

if ( !parsingSuccessful )

{

    // report to the user the failure and their locations in the document.

    std::cout  << "Failed to parse configuration\n"

               << reader.getFormattedErrorMessages();

    return;

}


// Get the value of the member of root named 'encoding', return 'UTF-8' if there is no

// such member.

std::string encoding = root.get("encoding", "UTF-8" ).asString();

// Get the value of the member of root named 'encoding', return a 'null' value if

// there is no such member.

const Json::Value plugins = root["plug-ins"];

for ( int index = 0; index < plugins.size(); ++index )  // Iterates over the sequence elements.

   loadPlugIn( plugins[index].asString() );

   

setIndentLength( root["indent"].get("length", 3).asInt() );

setIndentUseSpace( root["indent"].get("use_space", true).asBool() );


// ...

// At application shutdown to make the new configuration document:

// Since Json::Value has implicit constructor for all value types, it is not

// necessary to explicitly construct the Json::Value object:

root["encoding"] = getCurrentEncoding();

root["indent"]["length"] = getCurrentIndentLength();

root["indent"]["use_space"] = getCurrentIndentUseSpace();


Json::StyledWriter writer;

// Make a new JSON document for the configuration. Preserve original comments.

std::string outputConfig = writer.write( root );


// You can also use streams.  This will put the contents of any JSON

// stream at a particular sub-value, if you'd like.

std::cin >> root["subtree"];


// And you can write to a stream, using the StyledWriter automatically.

std::cout << root;

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  •  JSON入门级学习总结-JSON数据结构

    JSON入门级学习总结-JSON数据结构

    2016-02-25 11:05

  • 自定义jinja2 过滤器

    自定义jinja2 过滤器

    2016-02-14 10:00

  • Android解析Json速度最快的库:json

    Android解析Json速度最快的库:json

    2016-02-13 18:00

  • 带日期的bean转为json(bean-JSON)

    带日期的bean转为json(bean-JSON)

    2016-02-05 17:00

网友点评
'