JsonCpp——json文件的解析
定义:
官网: http://json.org/
在线解析器:http://json.cn/ http://www.bejson.com/
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它使得人们很容易的进行阅读和编写。同时也方便了机器进行解析和生成。它是基于 JavaScript Programming Language , Standard ECMA-262 3rd Edition - December 1999 的一个子集。 JSON 采用完全独
立于程序语言的文本格式,但是也使用了类 C 语言的习惯(包括 C, C++, C#, Java, JavaScript,Perl, Python 等)。这些特性使 JSON 成为理想的数据交换语言。
解释格式:
一个json实例
{
"animals": {
"dog": [
{"name": "Rufus", "age": 15},
{"name": "Marty","age": null}
],
"cat": [
{"name": "bosi","age": 15},
{"name": "maowang","age": null}
]
}
}
文档结构分析:
json 简单说就是 javascript 中的对象和数组,所以这两种结构就是对象和数组两种结构,通过这两种结构可以表示各种复杂的结构。
1)对象(Object)
对象在 js 中表示为“{}”括起来的内容, 数据结构为 {key: value,key: value,...}的键值对的结构, 在面向对象的语言中, key 为对象的属性, value 为对应的属性值, 所以很容易理解, 取值方法为 对象.key 获取属性值, 这个属性值的类型可以是 数字、字符串、 数组、 对象几种。

2)数组(Array)
数组在 js 中是中括号“[]”括起来的内容, 数据结构为 ["java","javascript","vb",...],取值方式和所有语言中一样, 使用索引获取, 字段值的类型可以是 数字、 字符串、数组、 对象几种。

3)数据类型
- 值(value)
值( value) 可以是双引号括起来的字符串( string) 、 数值(number)、 true、 false、null、 对象( object) 或者数组( array) 。 这些结构可以嵌套。

eg:“abc”, { “abc”:123} [1,2,3] true false null
- 字符串:
字符串( string) 是由双引号包围的任意数量 Unicode 字符的集合, 使用反斜线转义。 一个字符( character) 即一个单独的字符串( character string) 。 
eg:“abc” “\r\n” “u00A9”
- 数值
数值( number) 也与 C 或者 Java 的数值非常相似。 只是 JSON 的数值没有使用八进制与十六进制格式。 
eg:123 -123 1.234e5
JsonCpp
简介
JSON is a lightweight data-interchange format. It can represent numbers,strings, ordered sequences of values, and collections of name/value pairs.JsonCpp is a C++ library that allows manipulating JSON values, including serialization and deserialization to and from strings. It can also preserve existingstore user input files.
JsonCpp环境搭建:
下载地址:
https://github.com/open-source-parsers/jsoncpp#generating-amalgamated-source-and-header
Qt 中 JsonCpp 安装
① 解压 jsoncpp-master.zip 包
② 在根目录下, 运行 python amalgamate.py
③ 在根目录中生成 dist 文件夹包含三个文件 dist/json/json-forwards.h
dist/json/json.h dist/json.cpp
④ 在 Qt 工程目录下, 生成 json 文件夹, 并拷贝 json 目录下。
⑤ 在 Qt 工程中添加现有文件即可。
JSon 框架
Json::Value 是 jsoncpp 中最基本、最重要的类,用于表示各种类型的对象。
Json::Reader 用来将内存或文件中的 json 数据转换成 Json::Value 类型。
Json::Writer 用来将 Json::Value 类型转换成内存或文件中的 json 数据。
读写 Json
1)写:
#include <iostream>
#include <fstream>
#include "json/json.h"
using namespace Json;
using namespace std;
#if 0
{
"animals": {
"dog": [
{"name": "Rufus", "age": 15},
{"name": "Marty","age": null}
],
"cat": [
{"name": "bosi","age": 15},
{"name": "maowang","age": null}
]
}
}
#endif
void writeTestJson()
{
//对象的创建
Value map;
map["key1"] = ;
map["key2"] = "";
map["key3"] ;
//数组的创建
Value arr;
Value item;
for(int i=; i<; i++)
{
item["key"] = ;
item["key2"] = ;
arr.append(item);
}
string str = arr.toStyledString();
cout<<str;
} void writeJson()
{
Value dogArr;
Value Item;
Item["name"] = "Rufus";Item["age"] = ;
dogArr.append(Item);
Item["name"] = "Marty";Item["age"];
dogArr.append(Item);
Value catArr;
Item["name"] = "bosi";Item["age"] = ;
catArr.append(Item);
Item["name"] = "maowang";Item["age"];
catArr.append(Item);
Value rootValue;
rootValue["dog"] = dogArr;
rootValue["cat"] = catArr;
Value root;
root["anmials"] = rootValue;
string str = root.toStyledString();
cout<<str;
ofstream ofs("animal.json");
ofs<<str;
ofs.close();
} int main()
{
writeJson();
return ;
}
2)读
void readJson()
{
ifstream ifs("animal.json");
if(!ifs)
cout<<"open error"<<endl;
Value root;
Reader reader;
if(reader.parse(ifs,root))
{
cout<<root.toStyledString()<<endl;
Value &arr = root["animals"]["dog"];
for(int i=; i<arr.size(); i++)
{
cout<<"name"<<arr[i]["name"]<<endl;
cout<<"age "<<arr[i]["age"]<<endl;
}
arr = root["animals"]["cat"];
for(int i=; i<arr.size(); i++)
{
cout<<"name"<<arr[i]["name"]<<endl;
cout<<"age "<<arr[i]["age"]<<endl;
}
}
}
3)改
void readWriteJson()
{
ifstream ifs("animal.json");
if(!ifs)
cout<<"open error"<<endl;
Value root;
Reader reader;
if(reader.parse(ifs,root))
{
cout<<root.toStyledString()<<endl;
Value &arr = root["animals"]["dog"];
root["person"];
root["animals"]["tiger"];
root["animals"].removeMember("dog");
cout<<root.toStyledString()<<endl;
root.removeMember("animals");
cout<<root.toStyledString()<<endl;
}
} int main()
{
readWriteJson();
return ;
}
JsonCpp——json文件的解析的更多相关文章
- Json文件/网址解析
// // main.m // OC8-Json文件解析 // // Created by qianfeng on 15/6/23. // Copyright (c) 2015年 qianfeng. ...
- iOS开发网络篇-JSON文件的解析
一.什么是JSON数据 1.JSON的简单介绍 JSON:是一种轻量级的传输数据的格式,用于数据的交互 JSON是javascript语言的一个子集.javascript是个脚本语言(不需要编译),用 ...
- 在Unity中json文件的解析方式
using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using ...
- java读取json文件进行解析,String转json对象
String jsonFilePath = "C:/a.json"; File file = new File(jsonFilePath ); String input = Fil ...
- python读取json文件并解析
# -*- coding: utf-8 -*- import os import json import sys reload(sys) sys.setdefaultencoding('utf-8') ...
- IIS7.5支持解析读取.json文件数据
在站点中添加 MIME类型去支持Json文件的解析 添加mime类型 文件扩展名:.json MIME类型:application/json 添加成功后即可. 如果不能直接操作iis也可以直接在web ...
- java实现服务端守护进程来监听客户端通过上传json文件写数据到hbase中
1.项目介绍: 由于大数据部门涉及到其他部门将数据传到数据中心,大部分公司采用的方式是用json文件的方式传输,因此就需要编写服务端和客户端的小程序了.而我主要实现服务端的代码,也有相应的客户端的测试 ...
- 如何在Hadoop的MapReduce程序中处理JSON文件
简介: 最近在写MapReduce程序处理日志时,需要解析JSON配置文件,简化Java程序和处理逻辑.但是Hadoop本身似乎没有内置对JSON文件的解析功能,我们不得不求助于第三方JSON工具包. ...
- iOS 如何用JSONKit读写JSON文件
如何用JSONKit读写JSON文件 分类: ios2013-04-20 12:46 510人阅读 评论(0) 收藏 举报 JSON文件格式简单,使用方便,值得一用. 目前已经有多个库支持Json文 ...
随机推荐
- centos编译 Compiling FFmpeg on CentOS RHEL Fedora
This guide is based on a minimal installation of the latest CentOS release, and will provide a local ...
- POJ 2263 Heavy Cargo(ZOJ 1952)
最短路变形或最大生成树变形. 问 目标两地之间能通过的小重量. 用最短路把初始赋为INF.其它为0.然后找 dis[v]=min(dis[u], d); 生成树就是把最大生成树找出来.直到出发和终点能 ...
- Subversion基础:概念、安装、配置和基本操作(转)
本文转载至http://www.cnblogs.com/cokecoffe/archive/2012/06/01/2537130.html 转自:http://www.uml.org.cn/pzgl/ ...
- SAM4E单片机之旅——3、LED闪烁之定时器中断
让一个LED灯闪烁不过瘾,我们应该让这块开发板完成一点更高难度的任务:比如让两个LED灯闪烁. …… 当然了,以我们的现在使用的空循环技术,还是可以实现这点的.但是这样显得略为低端.所以我们使用一个高 ...
- @P0或@P1附近有语法错误
分析:@P0指的是第一个参数附近有错误;为'@P1'指的是第二个参数附近错误语法有错误.
- Hibernate总结(转)
原文:http://blog.csdn.net/yuebinghaoyuan/article/details/7300599 那我们看一下hibernate中整体的内容: 我们一一介绍其中的内容. H ...
- 找到bashrc
(1)直接sudo gedit ~/.bashrc就可以了,编辑完后关闭就行 (2)主文件夹下ctrl+h就能找到.bashrc文件 之所以要找到bashrc文件,是为了把命令 source /opt ...
- Jenkins安装部署及tomcat的入门介绍
这里我们使用的方法是用servlet容器来部署jenkins,使用的是tomcat 下载下来tomcat,解压 bin目录下存放的一些启动关闭批处理文件 conf目录下放的一些配置文件,配置虚拟主机之 ...
- 浏览器中的BOM和DOM
BOM 浏览器对象模型 提供了独立于内容而与浏览器窗口进行交互的对象.描述了与浏览器进行交互的方法和接口,可以对浏览器窗口进行访问和操作,譬如可以弹出新的窗口,改变状态栏中的文本,对Cookie的支持 ...
- (转)Java经典设计模式(1):五大创建型模式(附实例和详解)
原文出处: 小宝鸽 一.概况 总体来说设计模式分为三大类: (1)创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. (2)结构型模式,共七种:适配器模式.装饰器模式.代 ...