JavaCpp Master 的使用
因为在项目中要用json来进行cs间数据的传输(不敢苟同),特意贴出构造json的代码。固然下载jsoncpp mater后用cmake编译,将jsoncpp中的inclue和生成的lib放到对应的javascript
项目就能够使用了。java
如下为c++构造json的代码,构造的是leapmotion的TrackingHand的数据:c++
std::string json;
Json::Value JRoot;
Json::FastWriter JWrite;git
std::string Value;
JRoot["DeviceID"] = std::to_string(msg.device_id);
JRoot["HandNums"] = std::to_string(msg.tracking_event->nHands);
JRoot["FrameRate"] = std::to_string(msg.tracking_event->framerate);
JRoot["FrameID"] = std::to_string(msg.tracking_event->tracking_frame_id);json
// fill LEAP_FRAME_HEADER
Json::Value JInfo;
JInfo["Reserved"] = ""; // msg.tracking_event->info.reserved
JInfo["FrameID"] = std::to_string(msg.tracking_event->info.frame_id);
JInfo["TimeStamp"] = std::to_string(msg.tracking_event->info.timestamp);
JRoot["Info"] =JInfo;app
// fill LEAP_HAND
Json::Value JHand;
for (i = 0; i < msg.tracking_event->nHands; i++)
{
JHand.clear();
LEAP_HAND hand = msg.tracking_event->pHands[i];
JHand["HandID"] = std::to_string(hand.id);
JHand["HandFlag"] = std::to_string(hand.flags);
JHand["HandType"] = std::to_string(hand.type);
JHand["HandCfd"] = std::to_string(hand.confidence);
JHand["VisibleTime"] = std::to_string(hand.visible_time);
JHand["PchDis"] = std::to_string(hand.pinch_distance);
JHand["GrabAngle"] = std::to_string(hand.grab_angle);
JHand["PchStrth"] = std::to_string(hand.pinch_strength);
JHand["GrabStrth"] = std::to_string(hand.grab_strength);
JHand["HandID"] = std::to_string(hand.id);ide
//////////////////////////////////////////////////////////////////////////
// --- fill _LEAP_PALM ---
Json::Value JPalm;
Json::Value JVector;
// postion
JVector["HandX"] = std::to_string(hand.palm.position.x);
JVector["HandY"] = std::to_string(hand.palm.position.y);
JVector["HandZ"] = std::to_string(hand.palm.position.z);
JPalm["HandPos"] = JVector;post
// stabilized_position
JVector.clear();
JVector["StabX"] = std::to_string(hand.palm.stabilized_position.x);
JVector["StabY"] = std::to_string(hand.palm.stabilized_position.y);
JVector["StabZ"] = std::to_string(hand.palm.stabilized_position.z);
JPalm["StabPos"] = JVector;this
// velocity
JVector.clear();
JVector["VelocX"] = std::to_string(hand.palm.velocity.x);
JVector["VelocY"] = std::to_string(hand.palm.velocity.y);
JVector["VelocZ"] = std::to_string(hand.palm.velocity.z);
JPalm["Velocity"] = JVector;spa
// normal
JVector.clear();
JVector["NormalX"] = std::to_string(hand.palm.normal.x);
JVector["NormalY"] = std::to_string(hand.palm.normal.y);
JVector["NormalZ"] = std::to_string(hand.palm.normal.z);
JPalm["Normal"] = JVector;
// direction
JVector.clear();
JVector["DirX"] = std::to_string(hand.palm.direction.x);
JVector["DirY"] = std::to_string(hand.palm.direction.y);
JVector["DirZ"] = std::to_string(hand.palm.direction.z);
JPalm["Direction"] = JVector;
// orientation
JVector.clear();
JVector["OrientX"] = std::to_string(hand.palm.orientation.x);
JVector["OrientY"] = std::to_string(hand.palm.orientation.y);
JVector["OrientZ"] = std::to_string(hand.palm.orientation.z);
JVector["OrientW"] = std::to_string(hand.palm.orientation.w);
JPalm["Orient"] = JVector;
// hand width
JPalm["PalmWidth"] = std::to_string(hand.palm.width);
JHand["Palm"] = JPalm;
// --- fill _LEAP_PALM ---
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// fill LEAP_DIGIT digits[5];
Json::Value JDigit,JDigitValue;
std::string SDigitIndex = "Digit_";
for (int d = 0; d < 5; ++d)
{
JDigit.clear();
JDigitValue.clear();
// fill LEAP_BONE bones[4];
Json::Value JBone;
Json::Value JBoneValue;
std::string SBoneIndex = "Bone_";
for (int b = 0; b < 4; ++b)
{
// prev_joint
JBone.clear();
JVector.clear();
JVector["JPrevX"] = std::to_string(hand.digits[d].bones[b].prev_joint.x);
JVector["JPrevY"] = std::to_string(hand.digits[d].bones[b].prev_joint.y);
JVector["JPrevZ"] = std::to_string(hand.digits[d].bones[b].prev_joint.z);
JBoneValue["PrevJoint"] = JVector;
// next_joint
JVector.clear();
JVector["JNextX"] = std::to_string(hand.digits[d].bones[b].next_joint.x);
JVector["JNextY"] = std::to_string(hand.digits[d].bones[b].next_joint.y);
JVector["JNextZ"] = std::to_string(hand.digits[d].bones[b].next_joint.z);
JBoneValue["NextJoint"] = JVector;
// rotation
JVector.clear();
JVector["JRotX"] = std::to_string(hand.digits[d].bones[b].rotation.x);
JVector["JRotY"] = std::to_string(hand.digits[d].bones[b].rotation.y);
JVector["JRotZ"] = std::to_string(hand.digits[d].bones[b].rotation.z);
JVector["JRotW"] = std::to_string(hand.digits[d].bones[b].rotation.w);
JBoneValue["RotJonit"] = JVector;
// width
JBoneValue["BoneW"] = std::to_string(hand.digits[d].bones[b].width);
// append to array
std::string sIndex = SBoneIndex + std::to_string(b);
JBone[sIndex] = (JBoneValue);
JDigitValue["DigitBone"].append(JBone);
}
JDigitValue["DigitID"] = std::to_string(hand.digits[d].finger_id);
JDigitValue["DigitEx"] = std::to_string(hand.digits[d].is_extended);
std::string SDitIndex = SDigitIndex + std::to_string(d);
JDigit[SDitIndex] = JDigitValue;
JHand["Digits"].append(JDigit);
}
// fill LEAP_DIGIT digits[5];
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// fill LEAP_BONE arm
Json::Value JArm;
// prev_joint
JVector.clear();
JVector["JPrevX"] = std::to_string(hand.arm.prev_joint.x);
JVector["JPrevY"] = std::to_string(hand.arm.prev_joint.y);
JVector["JPrevZ"] = std::to_string(hand.arm.prev_joint.z);
JArm["PrevJoint"] = JVector;
// next_joint
JVector.clear();
JVector["JNextX"] = std::to_string(hand.arm.next_joint.x);
JVector["JNextY"] = std::to_string(hand.arm.next_joint.y);
JVector["JNextZ"] = std::to_string(hand.arm.next_joint.z);
JArm["NextJoint"] = JVector;
// rotation
JVector.clear();
JVector["JRotX"] = std::to_string(hand.arm.rotation.x);
JVector["JRotY"] = std::to_string(hand.arm.rotation.y);
JVector["JRotZ"] = std::to_string(hand.arm.rotation.z);
JVector["JRotW"] = std::to_string(hand.arm.rotation.w);
JArm["RotJonit"] = JVector;
// width
JArm["BoneW"] = std::to_string(hand.arm.width);
JHand["Arm"] = JArm;
// fill LEAP_BONE arm
//////////////////////////////////////////////////////////////////////////
JRoot["Hands"].append(JHand);
}
json = JWrite.write(JRoot);
最后想要查看构造的正确与否能够将代码中"json"变量的值拿出去验证(https://www.json.cn/)就好了。
再贴个解析出来的json串(最后有彩蛋):
{
"DeviceID":"1",
"FrameID":"538626",
"FrameRate":"115.338913",
"HandNums":"1",
"Hands":[
{
"Arm":{
"BoneW":"62.661057",
"NextJoint":{
"JNextX":"118.585686",
"JNextY":"194.915756",
"JNextZ":"53.336639"
},
"PrevJoint":{
"JPrevX":"222.295624",
"JPrevY":"98.940933",
"JPrevZ":"278.125885"
},
"RotJonit":{
"JRotW":"0.947595",
"JRotX":"0.151793",
"JRotY":"0.231619",
"JRotZ":"-0.159297"
}
},
"Digits":[
{
"Digit_0":{
"DigitBone":[
{
"Bone_0":{
"BoneW":"19.913471",
"NextJoint":{
"JNextX":"137.607010",
"JNextY":"202.271103",
"JNextZ":"23.301464"
},
"PrevJoint":{
"JPrevX":"137.607010",
"JPrevY":"202.271103",
"JPrevZ":"23.301464"
},
"RotJonit":{
"JRotW":"0.694175",
"JRotX":"0.134262",
"JRotY":"0.150038",
"JRotZ":"-0.691074"
}
}
},
{
"Bone_1":{
"BoneW":"19.913471",
"NextJoint":{
"JNextX":"146.965485",
"JNextY":"201.532410",
"JNextZ":"-25.010183"
},
"PrevJoint":{
"JPrevX":"137.607010",
"JPrevY":"202.271103",
"JPrevZ":"23.301464"
},
"RotJonit":{
"JRotW":"0.691491",
"JRotX":"0.063469",
"JRotY":"-0.071777",
"JRotZ":"-0.716002"
}
}
},
{
"Bone_2":{
"BoneW":"19.913471",
"NextJoint":{
"JNextX":"155.056320",
"JNextY":"199.788528",
"JNextZ":"-57.738071"
},
"PrevJoint":{
"JPrevX":"146.965485",
"JPrevY":"201.532410",
"JPrevZ":"-25.010183"
},
"RotJonit":{
"JRotW":"0.688240",
"JRotX":"0.068944",
"JRotY":"-0.102504",
"JRotZ":"-0.714888"
}
}
},
{
"Bone_3":{
"BoneW":"19.913471",
"NextJoint":{
"JNextX":"159.786896",
"JNextY":"198.534439",
"JNextZ":"-74.956009"
},
"PrevJoint":{
"JPrevX":"155.056320",
"JPrevY":"199.788528",
"JPrevZ":"-57.738071"
},
"RotJonit":{
"JRotW":"0.686345",
"JRotX":"0.071678",
"JRotY":"-0.117954",
"JRotZ":"-0.714059"
}
}
}
],
"DigitEx":"1",
"DigitID":"0"
}
},
{
"Digit_1":{
"DigitBone":[
{
"Bone_0":{
"BoneW":"19.021347",
"NextJoint":{
"JNextX":"101.719551",
"JNextY":"235.302353",
"JNextZ":"-41.144768"
},
"PrevJoint":{
"JPrevX":"125.933197",
"JPrevY":"209.063065",
"JPrevZ":"21.778065"
},
"RotJonit":{
"JRotW":"0.950221",