嵌入式Linux系统jsoncpp的移植

jsoncpp是c++实现对JSON格式数据的构建和解析,如果我们的应用程序的主框架是c++写的,那么建议采用该开源库;该开源库同样遵循MIT License,

通过github克隆仓库下来:

git clone https://github.com/open-source-parsers/jsoncpp.git

代码放在目录:…\tutorials\Patchs\4. jsoncpp移植

project目录中存放从该开源库裁剪出来的源文件还有我们的应用:

builder.cpp   构建JSON数据。

parser.cpp    解析JSON数据。

1 创建JSON实例

json目录中的json.h是最基础的对外API,我们需要包含该头文件;使用该开源库需要有C++基础,代码如下:

编译:

g++ -I. builder.cpp json_value.cpp json_reader.cpp json_writer.cpp -o json_builder

运行结果和用cJSON库结果一致。

2 解析JSON实例

直接上代码:

编译运行:

g++ -I. parser.cpp json_value.cpp json_reader.cpp json_writer.cpp -o json_parser

结果和用cJSON库是一致的。

jsoncpp函数分为两类,一种是取值的,一种是判断类型的:

  1. // 转换类型  
  2. Int asInt() const;  
  3. UInt asUInt() const;  
  4. Int64 asInt64() const;  
  5. UInt64 asUInt64() const;  
  6. LargestInt asLargestInt() const;  
  7. LargestUInt asLargestUInt() const;  
  8. float asFloat() const;  
  9. double asDouble() const;  
  10. bool asBool() const;  
  11. // 检测类型  
  12. bool isNull() const;  
  13. bool isBool() const;  
  14. bool isInt() const;  
  15. bool isInt64() const;  
  16. bool isUInt() const;  
  17. bool isUInt64() const;  
  18. bool isIntegral() const;  
  19. bool isDouble() const;  
  20. bool isNumeric() const;  
  21. bool isString() const;  
  22. bool isArray() const;  
  23. bool isObject() const;  

具体需要根据实际的应用调用相应的API即可。

 

==================================================================================================================================

如果觉得对您有帮助并想进一步深入学习交流可以扫描以下微信二维码或加入QQ群:928840648

欢迎共同学习成长,有一群爱学习的小伙伴一起勉励!!一起加油!!也可点击

 

笔者基于嵌入式系统框架内容如下整理编辑: