库:链接:https://pan.baidu.com/s/1UChrgqLPJxKopyqShDCHjg  密码:3yhz

#include <iostream>
#include <string>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h" using namespace rapidjson;
using namespace std; string readfile(const char *filename){
FILE *fp = fopen(filename, "rb");
if(!fp){
printf("open failed! file: %s", filename);
return "";
} char *buf = new char[*];
int n = fread(buf, , *, fp);
fclose(fp); string result;
if(n>=){
result.append(buf, , n);
}
delete []buf;
return result;
} int parseJSON(const char *jsonstr){
Document d;
if(d.Parse(jsonstr).HasParseError()){
printf("parse error!\n");
return -;
}
if(!d.IsObject()){
printf("should be an object!\n");
return -;
}
if(d.HasMember("errorCode")){
Value &m = d["errorCode"];
int v = m.GetInt();
printf("errorCode: %d\n", v);
}
printf("show numbers: \n");
if(d.HasMember("numbers")){
Value &m = d["numbers"];
if(m.IsArray()){
for(int i = ; i < m.Size(); i++){
Value &e = m[i];
int n = e.GetInt();
printf("%d,", n);
}
}
}
return ;
} int parseJSON2(const char *jsonstr){
Document d;
if(d.Parse(jsonstr).HasParseError()){
throw string("parse error!\n");
}
if(!d.IsObject()){
throw string("should be an object!\n");
}
if(!d.HasMember("errorCode")){
throw string("'errorCode' no found!");
} Value &m = d["errorCode"];
int v = m.GetInt();
printf("errorCode: %d\n", v);
printf("show numbers:\n");
if(d.HasMember("numbers")){
Value &m = d["numbers"];
if(m.IsArray()){
for(int i = ; i < m.Size(); i++){
Value &e = m[i];
int n = e.GetInt();
printf("%d", n);
}
}
}
return ;
} /*
//path="/Users/macname/Desktop/example.json" {
"errorCode":0,
"reason":"OK",
"result":{"userId":10086,"name":"中国移动"},
"numbers":[110,120,119,911]
} */
int main(){ string jsonstr = readfile("/Users/macname/Desktop/example.json");
//parseJSON(jsonstr.c_str()); try{
parseJSON2(jsonstr.c_str());
}catch(string e){
printf("error: %s \n", e.c_str());
}
getchar();
return ;
}

输出

errorCode:
show numbers:

c++ rapidjson读取json文件 解析的更多相关文章

  1. cocos2d-x 读取 json 文件并用 jsoncpp 做解析

    一码胜万言(请看注释) CclUtil.h // // CclUtil.h // PracticeDemo // // Created by kodeyang on 8/1/13. // // #if ...

  2. Unity的Json解析<一>--读取Json文件

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50373558 作者:car ...

  3. Asp.Net MVC 读取json文件

    有些系统上面的配置可以做成config里面的appsetting.这里要求写在json文件里面. 首先 添加命名空间 using Newtonsoft.Json; using System.IO; u ...

  4. Golang Json文件解析为结构体工具-json2go

    代码地址如下:http://www.demodashi.com/demo/14946.html 概述 json2go是一个基于Golang开发的轻量json文件解析.转换命令行工具,目前支持转换输出到 ...

  5. 读取json文件

    //读取json文件 var obji = File.ReadAllText(@"E:\test.json"); var resultdata = JsonConvert.Dese ...

  6. Json文件解析(下)

    Json文件解析(下) 代码地址:https://github.com/nlohmann/json   从STL容器转换 任何序列容器(std::array,std::vector,std::dequ ...

  7. Json文件解析(上)

    Json文件解析(上) 代码地址:https://github.com/nlohmann/json 自述文件 alt=GitHub赞助商 data-canonical-src="https: ...

  8. jquery无法读取json文件问题

    jquery无法读取json文件,如:user.json文件无法读取.把user.json文件的后缀名修改为aspx,文件内容不变,则可以读取~ 原理不懂!~~

  9. Unity 用C#脚本读取JSON文件数据

    读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON ...

随机推荐

  1. WebUploader 上传图片回显

    /* fileMaxCount 最大文件数 buttonText 按钮文本 multiple 是否多选 */ (function ($) { $.fn.extend({ uploadImg: func ...

  2. Spring循环依赖的三种方式以及解决办法

    一. 什么是循环依赖? 循环依赖其实就是循环引用,也就是两个或者两个以上的bean互相持有对方,最终形成闭环.比如A依赖于B,B依赖于C,C又依赖于A.如下图: 注意,这里不是函数的循环调用,是对象的 ...

  3. kvm第三章--虚拟化存储管理

  4. ext TreePanel树节点操作

    1.全部展开 tree.expandAll();2.全部收缩 tree.collapseAll();3.得到父节点 node.parentNode4.判断是否有父节点 node.parentNode= ...

  5. stm32内联汇编

    首先,先看一下mdk下的混合编程的基本方法: 使用如上方法就可以进行混合编程了. 但是要特殊注意一点,个人感觉这个是直接调用一个代码段,并非一个函数,因为他不会保护调用这个代码段之前的现场.比如: 在 ...

  6. javascript_13-函数是一种数据类型

    函数是一种数据类型 函数是一种数据类型 function var num =60; // 1 函数是一种数据类型 function var myFun = function(){ console.lo ...

  7. Vue框架之vuex的使用

    1.首先需要在你的项目目录下安装vuex 终端命令: 2.在全局组件中导入与声明vuex 3.创建store实例对象 let store = new Vuex.store({ state:{ }, m ...

  8. 运维开发笔记整理-template的使用

    运维开发笔记整理-Django的template的使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在上一篇博客中我们学习了HttpResponse 和JsonResponse方 ...

  9. 运维开发笔记整理-Request对象与Response对象

    运维开发笔记整理-Request对象与HttpResponse对象 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.request对象 1>.什么是request 首先,我 ...

  10. Java泛型全解析【接口、类、封装类型】

    目录   1.导读  2.为何需要泛型?   3.泛型的定义格式   3.泛型的好处  4.什么时候使用泛型?   5.泛型的擦除   6.泛型的补偿  7.泛型的应用      7.1[泛型类]   ...