C++解析JSON格式(JSONCpp库)
2015-09-06 19:45
531 查看
RoutePointFormatPtr结构(CommonFormat.h):
Functions.h
Functions.cpp
参考:
点击打开链接 /article/6956147.html
typedef struct _RoutePointFormat { _RoutePointFormat() { } double X; double Y; double Z; }RoutePointFormat, *RoutePointFormatPtr;
Functions.h
//Some functions. #ifndef _FUNCTIONS_H_ #define _FUNCTIONS_H_ #include "json/json.h" #ifdef _DEBUG #pragma comment(lib, "json_vc71_libmtd.lib") #else #pragma comment(lib, "json_vc71_libmt.lib") #endif // _DEBUG #include <vector> #include <string> #include "CommonFormat.h" std::vector<RoutePointFormatPtr> ReadJsonFromFile(const std::string &fileName); #endif //_FUNCTIONS_H_
Functions.cpp
#include "Functions.h" #include <iostream> #include <fstream> std::vector<RoutePointFormatPtr> ReadJsonFromFile( const std::string &fileName ) { std::vector<RoutePointFormatPtr> routePoint; Json::Reader reader;// 解析json用Json::Reader Json::Value root; // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array std::ifstream is; is.open (fileName, std::ios::binary ); if (reader.parse(is, root, false)) { long file_size = root["wayPoints"].size(); // 得到数组个数 for(long i = 0; i < file_size; ++i) // 遍历数组 { RoutePointFormatPtr pRoutePoint = new RoutePointFormat; Json::Value val_wayPoints = root["wayPoints"][i]; pRoutePoint->X = val_wayPoints["X"].asDouble(); pRoutePoint->Y = val_wayPoints["Y"].asDouble(); pRoutePoint->Z = val_wayPoints["Z"].asDouble(); routePoint.push_back(pRoutePoint); } } is.close(); return routePoint; }测试Json文件:
{ "wayPoints":[ {"X":0,"Y":1,"Z":2}, {"X":0,"Y":2,"Z":1}, {"X":1,"Y":2,"Z":0} ] }
参考:
点击打开链接 /article/6956147.html
相关文章推荐
- Effective C++——条款27(第5章)
- 牛客网C++错题本
- c++ primer 学习笔记-第十三章
- <二叉树 前中后 层序 非递归遍历 c语言实现>
- Effective C++——条款26(第5章)
- C++继承问题
- URL encode 与 URL decode 的C语言实现
- C++ 墙角知识 02
- 2015.09.06 C++的sizeof和strlen()
- 【C++】sublime
- 堆与栈的区别
- c++的vector使用:c++面试题:从cin读入一组词并把它们存入一个vector对象,然后设法把所有词都改写为大写字母
- C++中,虚函数,引用指针,拷贝复制等知识点总结
- 《C++ Primer 第五版》第1周:C++基础入门(第1章至第3章)
- C++入门经典 笔记 (第二十章)使用特殊的类、函数和指针
- 【C++】重载
- C语言判断给定的字符串是否为合法的ip地址
- WinPhone8.1 C++/CX引用C#的DLL陷阱
- C++中const 详解
- C语言点餐系统