赞
踩
在Unity中,如何将json格式的数据解析,并调用内部封装的数据呢?
在这里笔者用一个项目中的例子,即将摄像头捕获的人体关节点坐标json数据解析到Unity,再在Unity中输出数据的过程。
项目详情见全文
OpenPose的Unity3D实现
https://blog.csdn.net/BenJamin_Blue/article/details/86567246
需要解析的json数据如下:
{
"version": 1.2,
"people": [
{
"pose_keypoints_2d": [ 273.264, 38.5963, 0.869616, 301.865, 147.37, 0.80189, 219.778, 147.363, 0.74485, 177.866, 278.962, 0.660513, 149.317, 387.645, 0.783981, 389.568, 145.525, 0.69237, 418.295, 294.246, 0.581674, 429.69, 420.184, 0.744175, 225.593, 439.214, 0.393213, 0, 0, 0, 0, 0, 0, 357.144, 450.645, 0.348758, 0, 0, 0, 0, 0, 0, 252.327, 21.455, 0.845018, 296.116, 17.6204, 0.78486, 236.984, 38.5901, 0.239763, 330.499, 36.6363, 0.860353 ],
"face_keypoints_2d": [],
"hand_left_keypoints_2d": [ 430.255, 424.202, 0.255266, 416.676, 452.158, 0.343294, 421.469, 486.505, 0.0764938, 419.073, 497.688, 0.0160631, 424.664, 501.682, 0.00270473, 414.28, 496.091, 0.0290727, 396.707, 488.902, 0.00831178, 395.11, 489.7, 0.00435322, 413.481, 540.822, 0.00356947, 423.066, 497.688, 0.0233392, 411.884, 504.078, 0.00861702, 422.268, 503.28, 0.00597289, 415.878, 540.822, 0.00381597, 423.865, 496.091, 0.0400665, 412.682, 504.078, 0.0124277, 423.865, 503.28, 0.00940899, 412.682, 540.023, 0.00571543, 424.664, 486.505, 0.0412582, 395.11, 488.103, 0.0168368, 423.865, 501.682, 0.00908826, 424.664, 505.676, 0.00689844 ],
"hand_right_keypoints_2d": [ 159.561, 374.712, 0.0309012, 142.809, 387.822, 0.0350077, 142.809, 419.14, 0.0447348, 163.202, 436.62, 0.0462346, 149.364, 443.174, 0.0316575, 160.289, 403.845, 0.064782, 142.809, 430.065, 0.0809698, 153.734, 449.001, 0.128932, 164.659, 466.481, 0.114184, 160.289, 406.03, 0.0501139, 155.919, 427.88, 0.0803895, 163.202, 454.828, 0.162599, 169.757, 470.851, 0.186943, 161.746, 405.301, 0.0399239, 160.289, 424.966, 0.0734568, 165.387, 444.631, 0.164918, 170.486, 470.851, 0.246341, 166.116, 404.573, 0.0298664, 166.116, 423.51, 0.0586659, 169.757, 441.718, 0.0851868, 174.856, 465.753, 0.0832467 ],
"pose_keypoints_3d": [],
"face_keypoints_3d": [],
"hand_left_keypoints_3d": [],
"hand_right_keypoints_3d": []
}
]
}
由于只需搭建人体骨骼模型,在这里我们只需要解析出“people”中的“pose_keypoints_2d”数组数据,并将其在Unity的Console内输出即可。
using UnityEngine;
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Collections.Generic;
[Serializable]
public class ModelTest
{
public float version;
public List<People_sets> people;
}
[Serializable]
public class People_sets
{
public float[] pose_keypoints_2d;
}
using UnityEngine;
using System.IO;
using System.Text;
using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
public class JsonTest : MonoBehaviour
{
// Use this for initialization
void Start()
{
try
{
//解析过程
string jsonTest = File.ReadAllText(Application.dataPath + "/Resources/OpenPose/000000000468_keypoints.json", Encoding.UTF8);
ModelTest obj = JsonUtility.FromJson<ModelTest>(jsonTest);
for (int i = 0; i < 18; i++)
{
print("第" + i + "个点的x坐标= " + obj.people[0].pose_keypoints_2d[3 * i]);
print("第" + i + "个点的y坐标= " + obj.people[0].pose_keypoints_2d[3 * i + 1]);
}
}
catch (ArgumentOutOfRangeException)
{
}
}
// Update is called once per frame
void Update()
{
}
}
其中Application.dataPath + "/Resources/OpenPose/000000000468_keypoints.json"指json数据的路径+名称。
当然这些解析出来的数据还有多种用法,这里仅仅以输出文本为例。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。