JSON数据的Unity3D解析过程

(1) 问题描述:

在Unity中,如何将json格式的数据解析,并调用内部封装的数据呢?
在这里笔者用一个项目中的例子,即将摄像头捕获的人体关节点坐标json数据解析到Unity,再在Unity中输出数据的过程。

项目详情见全文
OpenPose的Unity3D实现
https://blog.csdn.net/BenJamin_Blue/article/details/86567246

(2) 问题分析:

需要解析的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内输出即可。

(3) 代码:

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数据的路径+名称。

(4) 运行结果:

在这里插入图片描述
当然这些解析出来的数据还有多种用法,这里仅仅以输出文本为例。

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值