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",
                                        "JRotX":"0.152326",
                                        "JRotY":"0.204764",
                                        "JRotZ":"-0.178740"
                                    }
                                }
                            },
                            {
                                "Bone_1":{
                                    "BoneW":"19.021347",
                                    "NextJoint":{
                                        "JNextX":"96.052589",
                                        "JNextY":"259.776794",
                                        "JNextZ":"-76.965714"
                                    },
                                    "PrevJoint":{
                                        "JPrevX":"101.719551",
                                        "JPrevY":"235.302353",
                                        "JPrevZ":"-41.144768"
                                    },
                                    "RotJonit":{
                                        "JRotW":"0.929702",
                                        "JRotX":"0.270840",
                                        "JRotY":"0.131468",
                                        "JRotZ":"-0.212169"
                                    }
                                }
                            },
                            {
                                "Bone_2":{
                                    "BoneW":"19.021347",
                                    "NextJoint":{
                                        "JNextX":"92.823044",
                                        "JNextY":"273.724518",
                                        "JNextZ":"-97.379700"
                                    },
                                    "PrevJoint":{
                                        "JPrevX":"96.052589",
                                        "JPrevY":"259.776794",
                                        "JPrevZ":"-76.965714"
                                    },
                                    "RotJonit":{
                                        "JRotW":"0.929702",
                                        "JRotX":"0.270840",
                                        "JRotY":"0.131468",
                                        "JRotZ":"-0.212169"
                                    }
                                }
                            },
                            {
                                "Bone_3":{
                                    "BoneW":"19.021347",
                                    "NextJoint":{
                                        "JNextX":"91.853699",
                                        "JNextY":"282.262634",
                                        "JNextZ":"-107.918884"
                                    },
                                    "PrevJoint":{
                                        "JPrevX":"92.823044",
                                        "JPrevY":"273.724518",
                                        "JPrevZ":"-97.379700"
                                    },
                                    "RotJonit":{
                                        "JRotW":"0.915986",
                                        "JRotX":"0.315168",
                                        "JRotY":"0.114676",
                                        "JRotZ":"-0.220199"
                                    }
                                }
                            }
                        ],
                        "DigitEx":"1",
                        "DigitID":"1"
                    }
                },
                {
                    "Digit_2":{
                        "DigitBone":[
                            {
                                "Bone_0":{
                                    "BoneW":"18.681490",
                                    "NextJoint":{
                                        "JNextX":"86.216492",
                                        "JNextY":"244.114365",
                                        "JNextZ":"-28.034405"
                                    },
                                    "PrevJoint":{
                                        "JPrevX":"117.004662",
                                        "JPrevY":"216.947342",
                                        "JPrevZ":"26.930801"
                                    },
                                    "RotJonit":{
                                        "JRotW":"0.944974",
                                        "JRotX":"0.186033",
                                        "JRotY":"0.254599",
                                        "JRotZ":"-0.087156"
                                    }
                                }
                            },
                            {
                                "Bone_1":{
                                    "BoneW":"18.681490",
                                    "NextJoint":{
                                        "JNextX":"71.838097",
                                        "JNextY":"276.732758",
                                        "JNextZ":"-63.019894"
                                    },
                                    "PrevJoint":{
                                        "JPrevX":"86.216492",
                                        "JPrevY":"244.114365",
                                        "JPrevZ":"-28.034405"
                                    },
                                    "RotJonit":{
                                        "JRotW":"0.912558",
                                        "JRotX":"0.328096",
                                        "JRotY":"0.205245",
                                        "JRotZ":"-0.132158"
                                    }
                                }
                            },
                            {
                                "Bone_2":{
                                    "BoneW":"18.681490",
                                    "NextJoint":{
                                        "JNextX":"63.252758",
                                        "JNextY":"296.209198",
                                        "JNextZ":"-83.909790"
                                    },
                                    "PrevJoint":{
                                        "JPrevX":"71.838097",
                                        "JPrevY":"276.732758",
                                        "JPrevZ":"-63.019894"
                                    },
                                    "RotJonit":{
                                        "JRotW":"0.912558",
                                        "JRotX":"0.328096",
                                        "JRotY":"0.205244",
                                        "JRotZ":"-0.132158"
                                    }
                                }
                            },
                            {
                                "Bone_3":{
                                    "BoneW":"18.681490",
                                    "NextJoint":{
                                        "JNextX":"58.834072",
                                        "JNextY":"306.233246",
                                        "JNextZ":"-94.661377"
                                    },
                                    "PrevJoint":{
                                        "JPrevX":"63.252758",
                                        "JPrevY":"296.209198",
                                        "JPrevZ":"-83.909790"                                    },                                    

"RotJonit":{                                        
"JRotW":"0.912559",                                        
"JRotX":"0.328094",                                        
"JRotY":"0.205244",                                        
"JRotZ":"-0.132157"                                    }                                }                            }                        ],                        




"DigitEx":"1",                        
"DigitID":"2"                    }                },                


{                    
"Digit_3":{                        
"DigitBone":[                            
{                                
"Bone_0":{                                    
"BoneW":"17.776623",                                    
"NextJoint":{                                        
"JNextX":"72.122719",                                        
"JNextY":"246.536057",                                        
"JNextZ":"-10.880052"                                    },                                    

"PrevJoint":{                                        
"JPrevX":"106.970520",                                        
"JPrevY":"220.599640",                                        
"JPrevZ":"32.793400"                                    },                                    

"RotJonit":{                                        
"JRotW":"0.923455",                                        
"JRotX":"0.213751",                                        
"JRotY":"0.315931",                                        
"JRotZ":"-0.041582"                                    }                                }                            },                            



{                                
"Bone_1":{                                    
"BoneW":"17.776623",                                    
"NextJoint":{                                        
"JNextX":"52.007629",                                        
"JNextY":"279.952332",                                        
"JNextZ":"-35.802479"                                    },                                    

"PrevJoint":{                                        
"JPrevX":"72.122719",                                        
"JPrevY":"246.536057",                                        
"JPrevZ":"-10.880052"                                    },                                    

"RotJonit":{                                        
"JRotW":"0.871180",                                        
"JRotX":"0.380198",                                        
"JRotY":"0.293645",                                        
"JRotZ":"-0.101331"                                    }                                }                            },                            



{                                
"Bone_2":{                                    
"BoneW":"17.776623",                                    
"NextJoint":{                                        
"JNextX":"39.347504",                                        
"JNextY":"300.984009",                                        
"JNextZ":"-51.488270"                                    },                                    

"PrevJoint":{                                        
"JPrevX":"52.007629",                                        
"JPrevY":"279.952332",                                        
"JPrevZ":"-35.802479"                                    },                                    

"RotJonit":{                                        
"JRotW":"0.871180",                                        
"JRotX":"0.380198",                                        
"JRotY":"0.293645",                                        
"JRotZ":"-0.101331"                                    }                                }                            },                            



{                                
"Bone_3":{                                    
"BoneW":"17.776623",                                    
"NextJoint":{                                        
"JNextX":"33.417343",                                        
"JNextY":"312.801575",                                        
"JNextZ":"-58.911327"                                    },                                    

"PrevJoint":{                                        
"JPrevX":"39.347504",                                        
"JPrevY":"300.984009",                                        
"JPrevZ":"-51.488270"                                    },                                    

"RotJonit":{                                        
"JRotW":"0.855216",                                        
"JRotX":"0.417098",                                        
"JRotY":"0.285057",                                        
"JRotZ":"-0.115660"                                    }                                }                            }                        ],                        




"DigitEx":"1",                        
"DigitID":"3"                    }                },                


{                    
"Digit_4":{                        
"DigitBone":[                            
{                                
"Bone_0":{                                    
"BoneW":"15.790586",                                    
"NextJoint":{                                        
"JNextX":"58.544788",                                        
"JNextY":"245.013306",                                        
"JNextZ":"6.507909"                                    },                                    

"PrevJoint":{                                        
"JPrevX":"95.469246",                                        
"JPrevY":"218.149841",                                        
"JPrevZ":"40.660465"                                    },                                    

"RotJonit":{                                        
"JRotW":"0.893491",                                        
"JRotX":"0.276956",                                        
"JRotY":"0.351888",                                        
"JRotZ":"0.033823"                                    }                                }                            },                            



{                                
"Bone_1":{                                    
"BoneW":"15.790586",                                    
"NextJoint":{                                        
"JNextX":"34.795624",                                        
"JNextY":"270.328033",                                        
"JNextZ":"-4.304240"                                    },                                    

"PrevJoint":{                                        
"JPrevX":"58.544788",                                        
"JPrevY":"245.013306",                                        
"JPrevZ":"6.507909"                                    },                                    

"RotJonit":{                                        
"JRotW":"0.805352",                                        
"JRotX":"0.427042",                                        
"JRotY":"0.411019",                                        
"JRotZ":"-0.010294"                                    }                                }                            },                            



{                                
"Bone_2":{                                    
"BoneW":"15.790586",                                    
"NextJoint":{                                        
"JNextX":"21.142355",                                        
"JNextY":"284.634979",                                        
"JNextZ":"-10.402311"                                    },                                    

"PrevJoint":{                                        
"JPrevX":"34.795624",                                        
"JPrevY":"270.328033",                                        
"JPrevZ":"-4.304240"                                    },                                    

"RotJonit":{                                        
"JRotW":"0.804531",                                        
"JRotX":"0.425591",                                        
"JRotY":"0.414175",                                        
"JRotZ":"-0.007869"                                    }                                }                            },                            



{                                
"Bone_3":{                                    
"BoneW":"15.790586",                                    
"NextJoint":{                                        
"JNextX":"11.997904",                                        
"JNextY":"294.241791",                                        
"JNextZ":"-14.450416"                                    },                                    

"PrevJoint":{                                        
"JPrevX":"21.142355",                                        
"JPrevY":"284.634979",                                        
"JPrevZ":"-10.402311"                                    },                                    

"RotJonit":{                                        
"JRotW":"0.803674",                                        
"JRotX":"0.426761",                                        
"JRotY":"0.414627",                                        
"JRotZ":"-0.008224"                                    }                                }                            }                        ],                        




"DigitEx":"1",                        
"DigitID":"4"                    }                }            ],            



"GrabAngle":"0.000000",            
"GrabStrth":"0.000000",            
"HandCfd":"1.000000",            
"HandFlag":"0",            
"HandID":"31",            
"HandType":"0",            
"Palm":{                
"Direction":{                    
"DirX":"-0.410584",                    
"DirY":"0.530247",                    
"DirZ":"-0.741795"                },                

"HandPos":{                    
"HandX":"92.287086",                    
"HandY":"227.650391",                    
"HandZ":"-8.786053"                },                

"Normal":{                    
"NormalX":"-0.447267",                    
"NormalY":"-0.826053",                    
"NormalZ":"-0.342913"                },                

"Orient":{                    
"OrientW":"0.916847",                    
"OrientX":"0.238087",                    
"OrientY":"0.269104",                    
"OrientZ":"-0.174035"                },                

"PalmWidth":"89.477859",                
"StabPos":{                    
"StabX":"0.000000",                    
"StabY":"0.000000",                    
"StabZ":"0.000000"                },                

"Velocity":{                    
"VelocX":"0.000000",                    
"VelocY":"0.000000",                    
"VelocZ":"0.000000"                }            },            


"PchDis":"86.311844",            
"PchStrth":"0.000000",            
"VisibleTime":"0"        }    ],    


"Info":{        
"FrameID":"538627",        
"Reserved":"",        
"TimeStamp":"1473312782870"    }}

 

事到现在再把ue4解析josn的代码也贴出来吧:

FString JsonData(ANSI_TO_TCHAR(arg));

        const TSharedRef<TJsonReader<TCHAR> > JsonReader = TJsonReaderFactory<>::Create(JsonData);
        //TSharedRef<TJsonReader<TCHAR>> JsonReader = FJsonStringReader::Create(MoveTemp(JsonData));

        TSharedPtr<FJsonObject> JSONSourceInfos;
        //TArray<TSharedPtr<FJsonValue>> JSONSourceInfos;
        if (!FJsonSerializer::Deserialize(JsonReader, JSONSourceInfos))
        {
            return false;
        }        

        // parse Json
        int32 Int32JsonValue = 0;
        int64 Int64JsonValue = 0;
        double FloatJsonValue = 0.0f;

        // get device ID 
        if (!JSONSourceInfos->TryGetNumberField(TEXT("DeviceID"), Int32JsonValue))
        {
            UE_LOG(LogTemp, Warning, TEXT("Parse Json Failed --- DeviceID"));
        }

        // --- fill LEAP_TRACKING_EVENT ---
        if (JSONSourceInfos->TryGetNumberField(TEXT("HandNums"), Int32JsonValue))
        {
            LeapTrackingPtr->nHands = Int32JsonValue;
        }

        // TODO : ?????
        if (Int32JsonValue <= 0)
            return false;

        if (JSONSourceInfos->TryGetNumberField(TEXT("FrameRate"), FloatJsonValue))
        {
            LeapTrackingPtr->framerate = (float)FloatJsonValue;
        }
        if (JSONSourceInfos->TryGetNumberField(TEXT("FrameID"), Int64JsonValue))
        {
            LeapTrackingPtr->tracking_frame_id = Int64JsonValue;
        }

        const TSharedPtr<FJsonObject>* JObject = nullptr;
        const TSharedPtr<FJsonObject>* JSubObject = nullptr;
        const TSharedPtr<FJsonObject>* JGrdObject = nullptr;

        const TArray< TSharedPtr<FJsonValue> >* JObjArray = nullptr;
        if (JSONSourceInfos->TryGetArrayField(TEXT("Hands"), JObjArray))
        {
            for (auto HeadObj : *JObjArray) 
            {
                JObject = &HeadObj->AsObject();

                if ((*JObject)->TryGetNumberField(TEXT("HandID"), Int32JsonValue))
                {
                    LeapTrackingPtr->pHands->id = Int32JsonValue;
                }
                if ((*JObject)->TryGetNumberField(TEXT("HandFlag"), Int32JsonValue))
                {
                    LeapTrackingPtr->pHands->flags = FloatJsonValue;
                }
                if ((*JObject)->TryGetNumberField(TEXT("HandType"), Int32JsonValue))
                {
                    LeapTrackingPtr->pHands->type = (eLeapHandType)Int32JsonValue;
                }
                if ((*JObject)->TryGetNumberField(TEXT("HandCfd"), FloatJsonValue))
                {
                    LeapTrackingPtr->pHands->confidence = (float)FloatJsonValue;
                }
                if ((*JObject)->TryGetNumberField(TEXT("VisibleTime"), Int64JsonValue))
                {
                    LeapTrackingPtr->pHands->visible_time = Int64JsonValue;
                }
                if ((*JObject)->TryGetNumberField(TEXT("PchDis"), FloatJsonValue))
                {
                    LeapTrackingPtr->pHands->pinch_distance = (float)FloatJsonValue;
                }
                if ((*JObject)->TryGetNumberField(TEXT("GrabAngle"), FloatJsonValue))
                {
                    LeapTrackingPtr->pHands->grab_angle = (float)FloatJsonValue;
                }
                if ((*JObject)->TryGetNumberField(TEXT("PchStrth"), FloatJsonValue))
                {
                    LeapTrackingPtr->pHands->pinch_strength = (float)FloatJsonValue;
                }
                if ((*JObject)->TryGetNumberField(TEXT("GrabStrth"), FloatJsonValue))
                {
                    LeapTrackingPtr->pHands->grab_strength = (float)FloatJsonValue;
                }
                if ((*JObject)->TryGetNumberField(TEXT("HandID"), Int32JsonValue))
                {
                    LeapTrackingPtr->pHands->id = Int32JsonValue;
                }

                // parse Palm
                if ((*JObject)->TryGetObjectField(TEXT("Palm"), JSubObject))
                {
                    (*JSubObject)->TryGetNumberField(TEXT("PalmWidth"), FloatJsonValue);
                    LeapTrackingPtr->pHands->palm.width = (float)FloatJsonValue;

                    // Hand Pos
                    if ((*JSubObject)->TryGetObjectField(TEXT("HandPos"), JGrdObject))
                    {
                        (*JGrdObject)->TryGetNumberField(TEXT("HandX"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.position.x = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("HandY"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.position.y = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("HandZ"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.position.z = FloatJsonValue;
                    }

                    // stabilized_position
                    if ((*JSubObject)->TryGetObjectField(TEXT("StabPos"), JGrdObject))
                    {
                        (*JGrdObject)->TryGetNumberField(TEXT("StabX"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.stabilized_position.x = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("StabY"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.stabilized_position.y = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("StabZ"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.stabilized_position.z = FloatJsonValue;
                    }

                    // velocity
                    if ((*JSubObject)->TryGetObjectField(TEXT("Velocity"), JGrdObject))
                    {
                        (*JGrdObject)->TryGetNumberField(TEXT("VelocX"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.velocity.x = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("VelocY"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.velocity.y = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("VelocZ"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.velocity.z = FloatJsonValue;
                    }

                    // normal
                    if ((*JSubObject)->TryGetObjectField(TEXT("Normal"), JGrdObject))
                    {
                        (*JGrdObject)->TryGetNumberField(TEXT("NormalX"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.normal.x = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("NormalY"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.normal.y = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("NormalZ"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.normal.z = FloatJsonValue;
                    }

                    // direction
                    if ((*JSubObject)->TryGetObjectField(TEXT("Direction"), JGrdObject))
                    {
                        (*JGrdObject)->TryGetNumberField(TEXT("DirX"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.direction.x = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("DirY"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.direction.y = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("DirZ"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.direction.z = FloatJsonValue;
                    }

                    // orientation
                    if ((*JSubObject)->TryGetObjectField(TEXT("Orient"), JGrdObject))
                    {
                        (*JGrdObject)->TryGetNumberField(TEXT("OrientX"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.orientation.x = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("OrientY"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.orientation.y = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("OrientZ"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.orientation.z = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("OrientW"), FloatJsonValue);
                        LeapTrackingPtr->pHands->palm.orientation.w = FloatJsonValue;
                    }
                }

                // parse Digit
                const TArray< TSharedPtr<FJsonValue> >* JSubArray = nullptr;
                if ((*JObject)->TryGetArrayField(TEXT("Digits"), JSubArray))
                {
                    int32 DitIndex = 0;
                    const TSharedPtr<FJsonObject>* JDitObj = nullptr;
                    for (auto DigitData : *JSubArray)
                    {
                        // do not use TryGetNumberField
                        const TSharedPtr<FJsonObject>& ValueObj = DigitData->AsObject();

                        if (ValueObj->TryGetObjectField(FString::Printf(TEXT("Digit_%d"), DitIndex), JDitObj))
                        {
                            (*JDitObj)->TryGetNumberField(TEXT("DigitID"), Int32JsonValue);
                            LeapTrackingPtr->pHands->digits[DitIndex].finger_id = Int32JsonValue;

                            (*JDitObj)->TryGetNumberField(TEXT("DigitEx"), Int32JsonValue);
                            LeapTrackingPtr->pHands->digits[DitIndex].is_extended = Int32JsonValue;

                            // LEAP_BONE Array[4]
                            const TArray< TSharedPtr<FJsonValue> >&JGrdArray = (*JDitObj)->GetArrayField(TEXT("DigitBone"));
                            if (JGrdArray.Num() > 0)
                            {
                                int32 BonIndex = 0;
                                const TSharedPtr<FJsonObject>* JBoneObj = nullptr;
                                for (auto BoneData : JGrdArray)
                                {
                                    const TSharedPtr<FJsonObject>& ValueBone = BoneData->AsObject();
                                    if (ValueBone->TryGetObjectField(FString::Printf(TEXT("Bone_%d"), BonIndex), JBoneObj))
                                    {

                                        // prev_joint
                                        (*JBoneObj)->TryGetObjectField(TEXT("PrevJoint"), JGrdObject);
                                        if (JGrdObject != nullptr)
                                        {
                                            (*JGrdObject)->TryGetNumberField(TEXT("JPrevX"), FloatJsonValue);
                                            LeapTrackingPtr->pHands->digits[DitIndex].bones[BonIndex].prev_joint.x = FloatJsonValue;
                                            (*JGrdObject)->TryGetNumberField(TEXT("JPrevY"), FloatJsonValue);
                                            LeapTrackingPtr->pHands->digits[DitIndex].bones[BonIndex].prev_joint.y = FloatJsonValue;
                                            (*JGrdObject)->TryGetNumberField(TEXT("JPrevZ"), FloatJsonValue);
                                            LeapTrackingPtr->pHands->digits[DitIndex].bones[BonIndex].prev_joint.z = FloatJsonValue;
                                        }

                                        // next_joint
                                        (*JBoneObj)->TryGetObjectField(TEXT("NextJoint"), JGrdObject);
                                        if (JGrdObject != nullptr)
                                        {
                                            (*JGrdObject)->TryGetNumberField(TEXT("JNextX"), FloatJsonValue);
                                            LeapTrackingPtr->pHands->digits[DitIndex].bones[BonIndex].next_joint.x = FloatJsonValue;
                                            (*JGrdObject)->TryGetNumberField(TEXT("JNextY"), FloatJsonValue);
                                            LeapTrackingPtr->pHands->digits[DitIndex].bones[BonIndex].next_joint.y = FloatJsonValue;
                                            (*JGrdObject)->TryGetNumberField(TEXT("JNextZ"), FloatJsonValue);
                                            LeapTrackingPtr->pHands->digits[DitIndex].bones[BonIndex].next_joint.z = FloatJsonValue;
                                        }

                                        // rotation
                                        (*JBoneObj)->TryGetObjectField(TEXT("RotJonit"), JGrdObject);
                                        if (JGrdObject != nullptr)
                                        {
                                            (*JGrdObject)->TryGetNumberField(TEXT("JRotX"), FloatJsonValue);
                                            LeapTrackingPtr->pHands->digits[DitIndex].bones[BonIndex].rotation.x = FloatJsonValue;
                                            (*JGrdObject)->TryGetNumberField(TEXT("JRotY"), FloatJsonValue);
                                            LeapTrackingPtr->pHands->digits[DitIndex].bones[BonIndex].rotation.y = FloatJsonValue;
                                            (*JGrdObject)->TryGetNumberField(TEXT("JRotZ"), FloatJsonValue);
                                            LeapTrackingPtr->pHands->digits[DitIndex].bones[BonIndex].rotation.z = FloatJsonValue;
                                            (*JGrdObject)->TryGetNumberField(TEXT("JRotW"), FloatJsonValue);
                                            LeapTrackingPtr->pHands->digits[DitIndex].bones[BonIndex].rotation.w = FloatJsonValue;
                                        }

                                        // width
                                        FloatJsonValue = (*JBoneObj)->GetNumberField(TEXT("BoneW"));
                                        LeapTrackingPtr->pHands->digits[DitIndex].bones[BonIndex].width = FloatJsonValue;
                                    }

                                    BonIndex++;
                                }
                            }
                        }                        
                        DitIndex++;
                    }
                }

                // parse Arm
                if ((*JObject)->TryGetObjectField(TEXT("Arm"), JSubObject))
                {
                    // prev_joint
                    if ((*JSubObject)->TryGetObjectField(TEXT("PrevJoint"), JGrdObject))
                    {
                        (*JGrdObject)->TryGetNumberField(TEXT("JPrevX"), FloatJsonValue);
                        LeapTrackingPtr->pHands->arm.prev_joint.x = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("JPrevY"), FloatJsonValue);
                        LeapTrackingPtr->pHands->arm.prev_joint.y = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("JPrevZ"), FloatJsonValue);
                        LeapTrackingPtr->pHands->arm.prev_joint.z = FloatJsonValue;
                    }

                    // next_joint
                    if ((*JSubObject)->TryGetObjectField(TEXT("NextJoint"), JGrdObject))
                    {
                        (*JGrdObject)->TryGetNumberField(TEXT("JNextX"), FloatJsonValue);
                        LeapTrackingPtr->pHands->arm.next_joint.x = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("JNextY"), FloatJsonValue);
                        LeapTrackingPtr->pHands->arm.next_joint.y = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("JNextZ"), FloatJsonValue);
                        LeapTrackingPtr->pHands->arm.next_joint.z = FloatJsonValue;
                    }

                    // rotation
                    if ((*JSubObject)->TryGetObjectField(TEXT("RotJonit"), JGrdObject))
                    {
                        (*JGrdObject)->TryGetNumberField(TEXT("JRotX"), FloatJsonValue);
                        LeapTrackingPtr->pHands->arm.rotation.x = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("JRotY"), FloatJsonValue);
                        LeapTrackingPtr->pHands->arm.rotation.y = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("JRotZ"), FloatJsonValue);
                        LeapTrackingPtr->pHands->arm.rotation.z = FloatJsonValue;
                        (*JGrdObject)->TryGetNumberField(TEXT("JRotW"), FloatJsonValue);
                        LeapTrackingPtr->pHands->arm.rotation.w = FloatJsonValue;
                    }

                    // width
                    (*JSubObject)->TryGetNumberField(TEXT("BoneW"), FloatJsonValue);
                    LeapTrackingPtr->pHands->arm.width = FloatJsonValue;
                }                
            }            
        }

 

若是你有幸看到了这里,我也想由衷的说一句:fuck!!!

posted @ 2020-12-19 13:47  5118会员优惠码  阅读(61)  评论(0编辑  收藏  举报

bug专家https://www.bugzj.com/

投资理财