使用jsoncpp解析生成json
在此站点下载jsoncpp(https://sourceforge.net/projects/jsoncpp/这个站点的版本较旧)
在电脑上安装Python,运行amalgamate.py,生成的dist目录中包含了我们需要的源文件和头文件
以下是使用jsoncpp解析与生成json的示例代码,
其中json_text.txt的内容为(关于此json数据的来源可参考此站点)
{
"face":
[
{
"attribute":
{
"age":27,
"gender":"Male",
"lefteye_opendegree":43,
"mouth_opendegree":2,
"pose":
{
"raise":-21,
"tilting":-2,
"turn":4
},
"righteye_opendegree":47
},
"face_id":"83e3edac3d5d49579089d3ee043d41ec",
"position":
{
"center":{"x":179,"y":50},
"eye_left":{"x":206,"y":78},
"eye_right":{"x":248,"y":78},
"height":94,"width":94
},
"tag":""
}
],
"img_height":500,
"img_id":"030-3aff983ea4ea5a35f89ae6",
"img_width":350,
"res_code":"0000",
"url":"http://e.hiphotos.baidu.com/baike/pic/item/9345d688d43f879428d347b3d81b0ef41bd53a7a.jpg"
}
jsoncpp_parse_test.cpp
#include <iostream>
#include <fstream>
#include "jsoncpp/json.h"
int main()
{
using namespace std;
using namespace Json;
ifstream ifs("json_text.txt");
if (ifs)
{
string line, jsonStr;
while (getline(ifs, line))
jsonStr.append(line);
CharReader *reader = CharReaderBuilder().newCharReader();
Value root;
JSONCPP_STRING errs;
if (reader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.size(), &root, &errs))
{
auto faceId = root["face"][]["face_id"].asString();
auto imgWidth = root["img_width"].asInt();
auto imgHeight = root["img_height"].asInt();
auto resCode = root["res_code"].asString();
auto url = root["url"].asString();
cout << "face_id:\t" << faceId << endl;
cout << "img_width:\t" << imgWidth << endl;
cout << "img_height:\t" << imgHeight << endl;
cout << "res_code:\t" << resCode << endl;
cout << "url:\t\t" << url << endl;
}
else
cout << errs << endl;
delete reader;
}
return ;
}
jsoncpp_generate_test.cpp
#include <iostream>
#include <memory>
#include "jsoncpp/json.h"
int main()
{
std::unique_ptr<Json::StreamWriter> writer(Json::StreamWriterBuilder().newStreamWriter());
Json::Value array, element;
element["face_id"] = "a9cebf8d5ae6fff514d8d2d8e07fa55b";
element["similarity"] = ;
element["img_id"] = "12.jpg";
element["people_name"] = "张艺谋";
array["result"].append(element);
element["face_id"] = "7f2de0e85bede3171c839d0dcabd849f";
element["similarity"] = 55.379097;
element["img_id"] = "6.jpg";
element["people_name"] = "曾国藩";
array["result"].append(element);
element["face_id"] = "40ebb31e8af7237a73dec9f242885a7e";
element["similarity"] = 52.59766;
element["img_id"] = "2.jpg";
element["people_name"] = "测试";
array["result"].append(element);
//std::ostringstream ss;
if (writer->write(array, &std::cout))
std::cout << "write failed" << std::endl;
else
std::cout << std::endl;
return ;
}
Compile.bat
g++ -std=c++11 jsoncpp_parse_test.cpp jsoncpp/jsoncpp.cpp -o jsoncpp_parse_test
g++ -std=c++11 jsoncpp_generate_test.cpp jsoncpp/jsoncpp.cpp -o jsoncpp_generate_test
使用jsoncpp解析生成json的更多相关文章
- PHP获取cookie、Token、模拟登录、抓取数据、解析生成json
本文介绍使用PHP获取cookie,获取Token.以及模拟登录.然后抓取数据.最后解析生成json的的过程. 0. 设置Cookie路径 set_time_limit(0); //使用的cookie ...
- jsoncpp解析非json串
转自:https://www.cnblogs.com/huojing/articles/5927488.html 由于Jsoncpp解析非法json时,会自动容错成字符类型.对字符类型取下标时,会触发 ...
- C#.Net 使用 JsonReader/JsonWriter 高性能解析/生成 Json 文档
Swifter.Json 是由本人编写的高性能且多功能的 Json 解析库.下图是 Swifter.Json 与 .Net 平台上的其他 Json 库性能对比: 在 Swifter.Json 近期更新 ...
- php生成json和js解析json
php生成json $value=array("name"=>"dfas");echo json_encode($value); js解析json var ...
- 认识Json解析json生成json
.markdown-body hr::after,.markdown-body::after { clear: both } .loopLine,.messageLine0 { } .markdown ...
- 运用 三种 原生 谷歌 阿里 解析和生成json
三种类生成JSON数据方法 JSON(原生): 第一种 JSONStringer和JSONObject区别在于添加对象时是按顺序添加的比如说 JSONStringer 添加 a:1 b:2 c:3那么 ...
- Json1:使用gson解析、生成json
Json解析: 1.json第三方解析包:json-lib.gson.jackson.fastjson等2.Google-gson只兼容jdk1.5版本以上:JSON-lib分别支持1.4和1.53. ...
- Android Json的使用(2) 使用Jackson解析和生成json
使用Jackson的三种方式 数据绑定模式:使用最方便 流模式:性能最佳 树模式:最灵活 以最常用的数据绑定模式为例 Json数据如下 { "name" : { "fir ...
- ruby json解析&生成
JSON 通常用于与服务端交换数据. 在接收服务器数据时一般是字符串. 我们可以使用 JSON.parse() 方法将数据转换为 ruby 对象. 一. json字符串解析 require 'json ...
随机推荐
- SoapUI offen used scripts
[ Groovy Step ] //get properties from testCase, testSuite and project in groovy step def testCasePro ...
- 【Luogu】2114起床困难综合征(位运算贪心)
题目链接 这题真是恶心死我了. 由于位运算每一位互不干涉,所以贪心由大到小选择每一位最优的解,但是要判断一下边界,如果选择该解使得原数>m则不能选择. 代码如下 #include<cstd ...
- UVa——1600Patrol Robot(A*或普通BFS)
Patrol Robot Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Descripti ...
- BZOJ [HNOI2015]亚瑟王 ——期望DP
发现每张卡牌最后起到作用只和是否打出去了有关. 而且每张牌打出去的概率和之前的牌打出去的情况有关. 所以我们按照牌的顺序进行DP. 然后记录$i$张牌中打出$j$张的概率,然后顺便统计答案. 直接对系 ...
- BZOJ 2298: [HAOI2011]problem a【动态规划】
Description 一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话(可能有相同的分数) Input 第一行一个整数n,接下来n行每行两个 ...
- .net面试题汇总一第一篇
1. 简述 private. protected. public. internal 修饰符的访问权限. private:私有成员,只能在类内部中才可以访问. protected:受保护的,只能在该类 ...
- Bayan 2015 Contest Warm Up D. CGCDSSQ (math,pair,map,暴力)
哎,只能转题解了,,, 8165031 2014-10-10 15:53:42 njczy2010 D - CGCDSSQ GN ...
- Laravel 报500错误
Laravel报500错误 发生情境: 使用Composer安装Laravel5.1版本到本地wamp环境,可以成功访问框架首页,然后上传到服务器上,报500错误. 解决: (1)在首页public/ ...
- Objective C语言中nil、Nil、NULL、NSNull的区别
以下内容是基于搜集整理的网上资料,供参考. nil:指向Objective C语言中对象的空指针,其定义值为(id)0. Nil:指向Objective C语言中类(Class)的空指针,其定义值为( ...
- 带你学Node系列之express-CRUD
前言 hello,小伙伴们,我是你们的pubdreamcc,本篇博文出至于我的GitHub仓库node学习教程资料,欢迎小伙伴们点赞和star,你们的点赞是我持续更新的动力. GitHub仓库地址:n ...