当前位置:   article > 正文

JSON数据的Unity3D解析过程_unity 解析json

unity 解析json

(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": []
    }
  ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

由于只需搭建人体骨骼模型,在这里我们只需要解析出“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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
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()
    {

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

其中Application.dataPath + "/Resources/OpenPose/000000000468_keypoints.json"指json数据的路径+名称。

(4) 运行结果:

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/123453
推荐阅读
相关标签
  

闽ICP备14008679号