库:链接: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. C# Winform 调试时某些项目不会自动重新生成

    右键启动项目→生成依赖性→项目依赖项 →勾选没有重新生成的项目

  2. ASP.NET Core 2.1 中的 HttpClientFactory (Part 1) HttpClientFactory介绍

    原文:https://www.stevejgordon.co.uk/introduction-to-httpclientfactory-aspnetcore  发表于:2018年1月 ASP.NET ...

  3. python实现ssh及sftp功能

    1.在Linux上我们通过scp命令实现主机间的文件传送,通过ssh实现远程登录 ,比如 我们经常使用的xshell远程登录工具,就是基础ssh协议实现window主机远程登录Linux主机 下面简单 ...

  4. 这些方面做好了才能叫运营-App运营日常

    现在APP的开发推广是时代的潮流,同时也是不少企业的难题.现在我们就来谈谈APP运营的一些问题. 1. 基础运营正常维护产品最日常.最普通的工作,如APP应用包在各大应用市场提交上传等,如安卓渠道,包 ...

  5. iview-admin本地测试上线登陆问题和文件路径找不到问题

    在项目中vue.config.js下修改上线路径(图中我修改为:根目录路径) 测试本地上线登陆出现问题: 在main.js下if (process.env.NODE_ENV !== 'producti ...

  6. MongoDB 创建数据库和查询数据

    1.选择数据库  use test  2.创建用户  db.createUser({user:"test01",pwd:"12345",roles:[{role ...

  7. 如何修改配置文件:CentOS下SSH端口修改

    CentOS各发行版中SSH端口默认为22,如果正式做站或其它用途,为了提高安全性就需要修改掉默认的SSH端口号,防止被有心人穷举密码.部分VPS提供商,若您的VPS服务器SSH遭受多次的暴力破解,可 ...

  8. Mybatis3.1-[tp_32-33]-_映射文件_select_resultMap关联查询_association分步查询_延迟加载

    笔记要点出错分析与总结 工程组织 1.定义接口 DepartmentMapper package com.dao; import com.bean.Department; public interfa ...

  9. 【HDU-1045,Fire Net-纯暴力简单DFS】

    原题链接:点击!   大致题意:白块表示可以放置炮台的位置——每个炮台可以攻击到上下左右的直线上的炮台(也就是说在它的上下左右直线上不可以再放置炮台,避免引起互相攻击),黑块表示隔离墙的位置——不可放 ...

  10. 从groupby 理解mapper-reducer

    注,reduce之前已经shuff. mapper.py #!/usr/bin/env python """mapper.py""" imp ...