使用jsoncpp读取json文件中的数组

使用jsoncpp读入json文件,
json文件和生成的二进制文件放在同目录下。
json文件内容:{
“version”: 1.3,
“people”: [
{
“person_id”: [-1],
“pose_keypoints_2d”: [
341.849,
305.227,
0.920068,
347.155,
368.056,
0.94722,
292.014,
370.81,
0.824518,
221.196,
292.018,
0.898937,
276.223,
221.234,
0.85556,
404.858,
360.284,
0.806604,
454.902,
265.776,
0.907669,
389.127,
218.472,
0.903598,
354.988,
567.72,
0.694311,
315.615,
570.35,
0.681895,
289.417,
722.617,
0.76646,
268.397,
848.75,
0.830318,
394.399,
562.508,
0.687771,
425.959,
712.114,
0.791497,
452.154,
848.609,
0.85849,
323.592,
299.89,
0.954254,
349.726,
294.567,
0.875772,
307.841,
310.374,
0.839133,
368.213,
302.593,
0.986262,
481.04,
890.768,
0.820154,
491.616,
880.187,
0.827631,
446.839,
854,
0.754842,
242.093,
890.715,
0.799698,
234.25,
877.564,
0.752977,
281.425,
859.276,
0.705694
],
“face_keypoints_2d”: [
310.032,
304.839,
0.85311,
312.111,
311.966,
0.875917,
314.189,
319.094,
0.896595,
316.565,
326.221,
0.852866,
319.535,
333.051,
0.836774,
324.287,
338.397,
0.853551,
330.226,
342.852,
0.76039,
337.353,
345.227,
0.79463,
345.372,
345.227,
0.846907,
352.499,
343.148,
0.866273,
358.736,
338.397,
0.797482,
363.784,
333.051,
0.782801,
366.754,
326.221,
0.880298,
368.536,
319.094,
0.821234,
369.13,
311.966,
0.887462,
369.13,
304.542,
0.868752,
369.13,
296.821,
0.832325,
314.189,
295.039,
0.778974,
316.268,
290.584,
0.831195,
321.317,
288.505,
0.875661,
326.365,
289.099,
0.887274,
331.117,
290.881,
0.909041,
342.699,
288.505,
0.824381,
347.154,
286.13,
0.861532,
351.608,
284.348,
0.830045,
356.954,
284.051,
0.82774,
360.517,
287.317,
0.752746,
337.947,
296.524,
0.817084,
338.244,
301.572,
0.867163,
338.541,
306.621,
0.840087,
339.135,
311.372,
0.830077,
333.79,
314.639,
0.956102,
336.759,
314.936,
0.861056,
340.323,
316.124,
0.861417,
342.996,
314.639,
0.993876,
345.669,
312.857,
0.91106,
320.723,
300.384,
0.889718,
323.396,
298.008,
0.904582,
326.662,
297.712,
0.973322,
329.632,
299.196,
0.824456,
326.662,
300.384,
0.967777,
323.693,
300.681,
0.911439,
345.966,
296.821,
0.838099,
349.232,
294.742,
0.837869,
352.796,
294.445,
0.901489,
355.469,
295.633,
0.927535,
352.796,
297.118,
0.927874,
349.826,
297.415,
0.899939,
331.117,
324.439,
0.892689,
333.79,
321.172,
0.90364,
337.947,
319.391,
0.910304,
340.917,
319.391,
0.950929,
343.59,
318.797,
0.83282,
348.341,
319.391,
0.891102,
353.093,
321.766,
0.849871,
350.42,
327.112,
0.844589,
346.56,
330.379,
0.818586,
342.996,
330.973,
0.971812,
338.838,
330.973,
0.896893,
334.681,
328.894,
0.872707,
332.899,
324.439,
0.880213,
338.244,
321.766,
0.930351,
340.917,
321.766,
0.89408,
345.075,
321.469,
0.817451,
352.202,
322.36,
0.828143,
345.669,
326.518,
0.913523,
342.402,
327.409,
0.847447,
338.541,
326.815,
0.878037,
324.881,
298.602,
0.815409,
350.717,
295.336,
0.937589
],
“hand_left_keypoints_2d”: [
385.468,
218.946,
0.810561,
377.622,
224.046,
0.781555,
365.46,
229.538,
0.915506,
353.691,
231.892,
0.756211,
343.883,
235.423,
0.57584,
352.906,
215.808,
0.384269,
340.745,
227.969,
0.610909,
335.645,
235.815,
0.669842,
331.33,
241.7,
0.667051,
352.514,
213.846,
0.710229,
340.353,
226.4,
0.712057,
335.253,
235.423,
0.645649,
328.976,
241.7,
0.590065,
354.083,
213.454,
0.851197,
343.883,
224.831,
0.742607,
337.607,
234.246,
0.65106,
332.507,
240.915,
0.53852,
357.614,
215.023,
0.888363,
347.806,
222.869,
0.741476,
345.453,
228.754,
0.825872,
341.922,
235.031,
0.690976
],
“hand_right_keypoints_2d”: [
280.003,
224.303,
0.883386,
289.33,
230.132,
0.842985,
302.155,
233.63,
1.01753,
311.871,
234.019,
0.568264,
317.701,
234.407,
0.643738,
308.374,
214.975,
0.568283,
319.644,
227.023,
0.720257,
323.919,
234.407,
0.747662,
327.028,
239.848,
0.760966,
307.596,
213.421,
0.895064,
320.033,
225.08,
0.762767,
323.919,
233.242,
0.82885,
328.194,
239.848,
0.561631,
304.487,
213.809,
0.816806,
316.535,
223.914,
0.650406,
320.81,
231.687,
0.574316,
326.64,
237.517,
0.480839,
299.435,
216.141,
0.850893,
309.151,
221.971,
0.781733,
313.426,
227.023,
0.883856,
317.312,
232.853,
0.666565
],
“pose_keypoints_3d”: [],
“face_keypoints_3d”: [],
“hand_left_keypoints_3d”: [],
“hand_right_keypoints_3d”: []
}
]
}
读取的demo:
#include <json/json.h>
#include
#include
#include
#include
using namespace std;
double inputpoint[134];
double modulepoint[134];
double createArr(char * m_filepath, string status)//status是一哥状态判断
{
Json::Reader reader;
Json::Value personpose;
ifstream in(m_filepath, ios::binary);
if (!in.is_open())
{
cout << “Error opening file\n”;
};
if (reader.parse(in, personpose))
{
double version = personpose[“version”].asDouble();
int posesize = personpose[“people”][0][“pose_keypoints_2d”].size();
// pose point arr
int mposesize = posesize/32;
double posepoint[mposesize];
for(int i=0,index=0;i<posesize;i++)
{
if ((i+1)%3 != 0)
{
posepoint[index] = personpose[“people”][0][“pose_keypoints_2d”][i].asDouble();
index++;
}
}
int handleftsize = personpose[“people”][0][“hand_left_keypoints_2d”].size();
int mhandleftsize = handleftsize/3
2;
double handleftpoint[mhandleftsize];
for(int n=0,index=0;n<handleftsize;n++)
{
if ((n+1)%3 != 0)
{
handleftpoint[index] = personpose[“people”][0][“hand_left_keypoints_2d”][n].asDouble();
index++;
}
}
// hand right point arr
int handrightsize = personpose[“people”][0][“hand_right_keypoints_2d”].size();
int mhandrightsize = handrightsize/32;
double handrightpoint[mhandrightsize];
for(int p=0,index=0;p<handrightsize;p++)
{
if ((p+1)%3 != 0)
{
handrightpoint[index] = personpose[“people”][0][“hand_right_keypoints_2d”][p].asDouble();
index++;
}
}
int index=0,loop;
loop = posesize/3
2+handleftsize/32+handrightsize/32;
cout << "loop: " << loop << endl;
//double onepersonpose[loop];
if(status == “input”){
for(int i=0;i<mposesize;i++)
{
inputpoint[index] = posepoint[i];
index++;
}
for(int i=0;i<mhandleftsize;i++)
{
inputpoint[index] = handleftpoint[i];
index++;
}
for(int i=0;i<mhandrightsize;i++)
{
inputpoint[index] = handrightpoint[i];
index++;
}
cout << inputpoint[loop-1] << endl;
}else{
for(int i=0;i<mposesize;i++)
{
modulepoint[index] = posepoint[i];
index++;
}
for(int i=0;i<mhandleftsize;i++)
{
modulepoint[index] = handleftpoint[i];
index++;
}
for(int i=0;i<mhandrightsize;i++)
{
modulepoint[index] = handrightpoint[i];
index++;
}
cout << modulepoint[loop-1] << endl;
}
};
}
int main(int argc, char *argv[])
{
// Parsing command line flags
gflags::ParseCommandLineFlags(&argc, &argv, true);
char *filepath = argv[1];
cout << “open json file:” << filepath << endl;
createArr(filepath, “input”);
cout << "inputpoint[136][1]: " << inputpoint[273] << endl;
char *module = argv[2];
createArr(module,“model”);
cout << "module[273]: " << modulepoint[273] << endl;
return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要解析JSON文件,首先需要安装jsoncpp库,可以通过以下命令安装: ``` sudo apt-get install libjsoncpp-dev ``` 安装完毕后,就可以使用jsoncpp库来解析JSON文件了。下面是一个简单的示例程序: ```cpp #include <iostream> #include <fstream> #include <jsoncpp/json/json.h> using namespace std; int main() { ifstream ifs("example.json"); Json::Reader reader; Json::Value root; if (!reader.parse(ifs, root)) { cout << "Failed to parse JSON" << endl; return 1; } string name = root["name"].asString(); int age = root["age"].asInt(); cout << "Name: " << name << endl; cout << "Age: " << age << endl; return 0; } ``` 在这个示例程序,首先通过ifstream读取example.json文件,然后使用Json::Reader来解析JSON文件,解析结果存放在Json::Value对象root。可以通过root对象来访问JSON的数据。 在这个示例程序,我们访问了JSON的"name"和"age"字段,并将它们打印出来。 需要注意的是,以上示例程序JSON文件格式如下: ```json { "name": "John", "age": 30 } ``` 如果JSON文件的格式不正确,解析过程可能会出现错误。因此,在实际应用,需要根据实际情况进行错误处理。 ### 回答2: JSONCpp是一个C++的库,用于解析和生成JSON格式的数据。它提供了一组简单的API来读取和修改JSON数据。 使用JSONCpp解析JSON文件的过程包括以下几个步骤: 1. 引入JSONCpp库:在编程环境,需要将JSONCpp文件引入到项目。可以从JSONCpp的官方网站上下载并配置库文件。 2. 打开JSON文件使用JSONCpp的`Json::CharReader`来打开JSON文件,将其读取为一个字符串。 3. 解析JSON文件使用JSONCpp的`Json::Value`来解析JSON字符串。可以使用`Json::Reader`的`parse()`方法来将JSON字符串转换为`Json::Value`对象。 4. 提取JSON数据:使用`Json::Value`对象的成员访问运算符`[]`来获取JSON数据。可以使用`isMember()`方法判断指定的成员是否存在,使用`asString()`、`asInt()`等方法获取指定成员的值。 5. 遍历JSON数据:如果JSON数据是一个数组类型,可以使用`size()`方法获取数组的长度,使用`operator[]`来访问数组的元素。 6. 修改JSON数据:使用`Json::Value`对象的成员访问运算符`[]`来修改JSON数据。可以使用`append()`方法向数组类型的JSON数据添加元素。 7. 保存JSON文件使用JSONCpp的`Json::StyledWriter`对象将`Json::Value`对象转换为字符串,并将其写入文件。 总结起来,使用JSONCpp解析JSON文件的过程可以归纳为:引入库文件、打开JSON文件、解析JSON文件、提取和操作JSON数据、保存JSON文件。这样就可以实现对JSON文件读取和解析操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值