C++ 处理JSON学习记录,今天整理了一下,把测试代码全文发上来。
JSONCPP 官方地址:http://jsoncpp.sourceforge.net/
// study_json.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <json/json.h>
#include <string>
#if defined(_MSC_VER) && _MSC_VER >= 1310
# pragma warning( disable: 4996 ) // disable deprecation warning
#endif
using namespace std;
//打印任意json数据(类似php的print_r函数) 2012-6-30 by Dewei
//用法:
//#include <json/json.h>
//#include <string>
//using namespace std;
//string jsontxt = "{\"key_array\":[\"array_string\",\"123456\"],\"key_bool\":true,\"key_number\":\"123456789\",\"key_object\":{\"firtName\":\"Dewei\",\"lastName\":\"Zhu\"},\"key_string\":\"字符串测试\"}";
//Json::Reader reader;
//Json::Value jsobject;
//reader.parse(jsontxt, jsobject);
//print_json(jsobject);
void print_json(Json::Value &jsobject)
{
if (jsobject.empty()|| (!jsobject.isArray() && !jsobject.isObject()))
return;
for (Json::Value::iterator iter = jsobject.begin(); iter != jsobject.end(); ++iter)
{
Json::Value current_key = iter.key();
int num_size = 0;
if (current_key.isInt())
num_size = jsobject[current_key.asInt()].size();
else if (current_key.isString())
num_size = jsobject[current_key.asString()].size();
if (num_size != 0) {
std::cout << "\r\n memberName:" << iter.memberName() << std::endl;
print_json(*iter);
}
else {
//std::cout << " key:" << current_key << " index:" << iter.index()<<" value:"<< *iter << std::endl;
std::cout << " key:" << current_key <<" value:"<< *iter << std::endl;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//生成json字符串
Json::Value temp;
temp["firtName"] = "Dewei";
temp["lastName"] = "Zhu";
Json::Value root;
root["key_string"] = Json::Value("字符串测试");
root["key_number"] = Json::Value("123456789");
root["key_bool"] = Json::Value(true);
root["key_object"] = temp;
root["key_array"].append("array_string");
root["key_array"].append("123456");
//Json::ValueType type = root.type();
//将Json::Value 对象转换为字符串
Json::FastWriter fast_writer;
std::string jsontext = fast_writer.write(root);
std::cout << jsontext << std::endl;
//将字符串转换为 Json::Value 对象
std::string jsontxt = jsontext;
Json::Reader reader;
Json::Value jsobject;
reader.parse(jsontxt, jsobject);
print_json(jsobject);
//std::cout << jsobject["key_object"] << std::endl;
getchar();
return 0;
}
分享到:
相关推荐
jsoncpp-0_c++jsoncpp_jsoncpp_jsoncpp-0.10.7_jsoncpp.lib_源码.zip
jsoncpp-0_c++jsoncpp_jsoncpp_jsoncpp-0.10.7_jsoncpp.lib_源码.rar
JsonCpp is a C++ library that allows manipulating JSON values includingserialization and deserialization to and from strings. It can also preserveexisting comment in unserialization/serialization ...
JSONCPP是一个C++库,允许操作JSON值,包括字符串的序列化和反序列化。它还可以在非序列化/序列化步骤中保留现有注释,使其成为存储用户输入文件的方便格式。
Jsoncpp_可正常编译运行_包括makefile文件(记不清版本,可去如下路径下载最新版本) 下载地址:https://github.com/open-source-parsers/jsoncpp
jsoncpp修复版,完美支持中文(多字节),从写入到读取都完美支持,自己根据官网文档来编译
jsoncpp源代码工程,可直接编译使用,最完整的工程。
jsoncpp解析\u中文解析不出来,修改源码jsoncpp-0.6.0-rc2 完美解决问题。
jsoncpp。json_vc71_libmtd.lib和include。解决方案配置debug+x64。
c/c++ json解析库,嵌入式 linux可用
不抛弃不放弃,此乃王者之精神,加油吧,少年
编译好的jsoncpp二进制库文件,版本1.8.4 适用于windows 64位平台 包括64和32位、及Debug和Release版共4个版本 目录树: |--x86 动态链接库文件 |--bin --|--x64 | jsoncpp-|--include 头文件 | |--...
基于 jsoncpp-src-0.6.0-rc2 修改 解决读时对"\uXXXX"无法解析的问题 解决写时中文字符未转义保存的问题
jsoncpp 的静态链接库
使用Jsoncpp包中的.cpp和.h文件放入项目中用,新建项目之后,将jsoncpp-master\include\json下的json目录直接复制到项目目录下,将jsoncpp-master\src\lib_json下面的json_reader.cpp、json_tool.h、json_value.cpp...
jsoncpp 开发库,包括include 文件、vs2008下可用的 lib文件、vs2010下可用的lib文件
jsoncpp v0.6 支持 UInt64(long),很好很强大!
jsoncpp
cocos2dx_helloworld_+jsoncpp