此站点下载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的更多相关文章

  1. PHP获取cookie、Token、模拟登录、抓取数据、解析生成json

    本文介绍使用PHP获取cookie,获取Token.以及模拟登录.然后抓取数据.最后解析生成json的的过程. 0. 设置Cookie路径 set_time_limit(0); //使用的cookie ...

  2. jsoncpp解析非json串

    转自:https://www.cnblogs.com/huojing/articles/5927488.html 由于Jsoncpp解析非法json时,会自动容错成字符类型.对字符类型取下标时,会触发 ...

  3. C#.Net 使用 JsonReader/JsonWriter 高性能解析/生成 Json 文档

    Swifter.Json 是由本人编写的高性能且多功能的 Json 解析库.下图是 Swifter.Json 与 .Net 平台上的其他 Json 库性能对比: 在 Swifter.Json 近期更新 ...

  4. php生成json和js解析json

    php生成json $value=array("name"=>"dfas");echo json_encode($value); js解析json var ...

  5. 认识Json解析json生成json

    .markdown-body hr::after,.markdown-body::after { clear: both } .loopLine,.messageLine0 { } .markdown ...

  6. 运用 三种 原生 谷歌 阿里 解析和生成json

    三种类生成JSON数据方法 JSON(原生): 第一种 JSONStringer和JSONObject区别在于添加对象时是按顺序添加的比如说 JSONStringer 添加 a:1 b:2 c:3那么 ...

  7. Json1:使用gson解析、生成json

    Json解析: 1.json第三方解析包:json-lib.gson.jackson.fastjson等2.Google-gson只兼容jdk1.5版本以上:JSON-lib分别支持1.4和1.53. ...

  8. Android Json的使用(2) 使用Jackson解析和生成json

    使用Jackson的三种方式 数据绑定模式:使用最方便 流模式:性能最佳 树模式:最灵活 以最常用的数据绑定模式为例 Json数据如下 { "name" : { "fir ...

  9. ruby json解析&生成

    JSON 通常用于与服务端交换数据. 在接收服务器数据时一般是字符串. 我们可以使用 JSON.parse() 方法将数据转换为 ruby 对象. 一. json字符串解析 require 'json ...

随机推荐

  1. 刷题总结——shortest(ssoi)

    题目: 题目背景 SOURCE:NOIP2015-SHY-3 题目描述 给定一张 n 个点的有向带权完全图,和一个数组 a[] ,请按顺序删除数组中的点,请求出在删除点 a[i] 以前,所有未删除点对 ...

  2. 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

    http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...

  3. bzoj 5110 Yazid的新生舞会

    题目大意: 一个数列,求有多少个区间$[l,r]$满足该区间的众数出现次数大于$\lceil \frac{r-l}{2} \rceil$ 思路: 对于一个区间满足条件的众数明显是唯一的 所以设该数的前 ...

  4. JConsole手册

    一篇Sun官方网站上介绍JConsole使用的文章,前段时间性能测试的时候大概翻译了一下以便学习,今天整理一下发上来,有些地方也不知道怎么翻,就保留了原文,可能还好理解点,呵呵,水平有限,翻的不好,大 ...

  5. spring-Ioc容器与bean

    1. spring 的beanFactory容器 bean.xml  HelloWorld实体类与spring教程学习笔记1相同 public static void main(String[] ar ...

  6. Yii 之控制器响应

    public function actionIndex(){ //控制器响应处理 $res = \Yii::$app->response; //设置状态码 // $res->statusC ...

  7. Linux上安装使用SSH

    參考博客:http://blog.csdn.net/xqhrs232/article/details/50960520 Ubuntu安装使用SSH ubuntu默认并没有安装ssh服务,如果通过ssh ...

  8. MongoDB数据类型查询与修改

    MongoDB数据类型和对应的代码如下: MongoDB可以根据字段类型进行文档查询: 可以看到,friend集合的文档中,age字段有32位int类型的,也有double类型的.如果需要把doubl ...

  9. Day 9 Linux samba & ngnix

    (摘) Samba服务 一.Samba简介  Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服 ...

  10. springboot主要注解及其作用

    1.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...