c++ rapidjson读取json文件 解析
库:链接: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文件 解析的更多相关文章
- cocos2d-x 读取 json 文件并用 jsoncpp 做解析
一码胜万言(请看注释) CclUtil.h // // CclUtil.h // PracticeDemo // // Created by kodeyang on 8/1/13. // // #if ...
- Unity的Json解析<一>--读取Json文件
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50373558 作者:car ...
- Asp.Net MVC 读取json文件
有些系统上面的配置可以做成config里面的appsetting.这里要求写在json文件里面. 首先 添加命名空间 using Newtonsoft.Json; using System.IO; u ...
- Golang Json文件解析为结构体工具-json2go
代码地址如下:http://www.demodashi.com/demo/14946.html 概述 json2go是一个基于Golang开发的轻量json文件解析.转换命令行工具,目前支持转换输出到 ...
- 读取json文件
//读取json文件 var obji = File.ReadAllText(@"E:\test.json"); var resultdata = JsonConvert.Dese ...
- Json文件解析(下)
Json文件解析(下) 代码地址:https://github.com/nlohmann/json 从STL容器转换 任何序列容器(std::array,std::vector,std::dequ ...
- Json文件解析(上)
Json文件解析(上) 代码地址:https://github.com/nlohmann/json 自述文件 alt=GitHub赞助商 data-canonical-src="https: ...
- jquery无法读取json文件问题
jquery无法读取json文件,如:user.json文件无法读取.把user.json文件的后缀名修改为aspx,文件内容不变,则可以读取~ 原理不懂!~~
- Unity 用C#脚本读取JSON文件数据
读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON ...
随机推荐
- C/C++ cmake example
学习 Golang,有时需要 Cgo,所以需要学习 C.C++. 语言入门: https://item.jd.com/12580612.html https://item.jd.com/2832653 ...
- 在论坛中出现的比较难的sql问题:41(循环替换 循环替换关键字)
原文:在论坛中出现的比较难的sql问题:41(循环替换 循环替换关键字) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.
- JQuery实现密码可见不可见
在Html页面上实现密码可见不可见,使用的阿里巴巴矢量图标库. html部分: <!doctype html> <html> <head> <meta cha ...
- entity-framework-core – 实体框架核心RC2表名称复数
参考地址:https://docs.microsoft.com/zh-cn/ef/core/modeling/relational/tables http://www.voidcn.com/artic ...
- .net core 杂记:用Autofac替换内置容器
官方建议使用内置容器,但有些功能并不支持,如下: 属性注入 基于名称的注入 子容器 自定义生存期管理 Func<T> 支持 所以可以使用其他第三方IOC容器,如Autofac,下面为学习使 ...
- Flask基础原理
一.Flask简介 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架. Werkzeug的本质是Socket服务端,用于接收http请求并对请 ...
- body onload()事件和table insertRow()、tr insertCell()
onload事件: 定义和用法: onload 事件会在页面或图像加载完成后立即发生. onload 通常用于 <body> 元素,在页面完全载入后(包括图片.css文件等等.)执行脚本代 ...
- iOS开发-NSArray
忙了一上午,解决了几个bug,现在终于抽出来一点时间喝点水休息下, 想着系列这么浩大的一个工程,才刚刚开始写,不能断了,就跟写小说一样,既然是系列,那么就需要不停更... 好吧. 简单的说说iOS开发 ...
- linux IPC简单学习
Posix和system v区别 所谓的IPC(进程间通信)指的是消息队列,共享内存,信号量3种机制合并起来,当然,这是个狭义的概念,只包含这三种.IPC又可以分为system v进程间通信和posi ...
- ELK文档--ELK简介
请参考:http://www.cnblogs.com/aresxin/p/8035137.html