【Unity学习笔记】Unity使用JsonUtility解析Json(附注意事项)

Json实体类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class StudentData
{
    public string StudentName;
    public string StudentGender;
    public string StudentAge;

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Assets.UIFrameWork
{
    [System.Serializable]
   public class StudentDataItems
    {
        public StudentData[] infoList;
    }
}

Json

{
  "infoList": [
    {
      "StudentName": "张三",
      "StudentGender": "男",
      "StudentAge": "23"

    },
    {
      "StudentName": "李四",
      "StudentGender": "女",
      "StudentAge": "23"

    },
    {
      "StudentName": "王五",
      "StudentGender": "男",
      "StudentAge": "23"

    }
  ]
}

把Json文件放到名字为Resources的文件夹下
在这里插入图片描述

解析方法的代码

  public void ParseJsonToStr()
    {
        TextAsset ta = Resources.Load<TextAsset>("uiJson");
        byte[] ReadByte = Encoding.UTF8.GetBytes(ta.text);
        StudentDataItems dataItems = JsonUtility.FromJson<StudentDataItems>(UTF8Encoding.UTF8.GetString(ReadByte));
        foreach(StudentData studentData in dataItems.infoList)
        {
            Debug.Log("学生姓名为"+studentData.StudentName);
        }
        
    }

结果

在这里插入图片描述

注意事项

(1)用于接收的JSON实体类需要声明[Serializable] 序列化。
(2)使用Unity自带方法时,实体类如果是属性成员(public bool has_more{get;set;})的话,在序列化的时候会缺失这些成员,导致解析不出来。将属性改为字段即可。
(3)如何解析的对象是数组,自带的不能成功解析,可以人为将其封装为JSON对象。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity中有几种解析JSON的方法。其中比较流行的有以下几种: 1. 使用listJson解析数据:这种方法要求数据对象必须和JSON一对一对应。可以使用listJson库来解析JSON数据。解析方法如下: ```csharp public static T GetData2<T>(string text) where T : class { T t = JsonMapper.ToObject<T>(text); return t; } ``` 这种方法适用于数据对象和JSON字段一一对应的情况。 2. 使用Unity自带的JsonUtility解析数据:Unity自带了JsonUtility类,可以用来解析JSON数据。但是它不能解析复杂的JSON结构。解析方法如下: ```csharp public static T GetData2<T>(string text) where T : class { T t = JsonUtility.FromJson<T>(text); return t; } ``` 这种方法适用于简单的JSON结构。 3. 使用Newtonsoft.Json解析数据:Newtonsoft.Json是一个流行的第三方库,可以解析复杂的JSON结构,并且不需要和JSON字段一一对应,更加灵活。你可以从以下地址下载该库:\[https://github.com/SaladLab/Json.Net.Unity3D/releases\](https://github.com/SaladLab/Json.Net.Unity3D/releases)。解析方法如下: ```csharp public static T GetData2<T>(string text) where T : class { T t = JsonConvert.DeserializeObject<T>(text); return t; } ``` 这种方法适用于复杂的JSON结构。 以上是Unity解析JSON的几种方法,你可以根据自己的需求选择适合的方法来解析JSON数据。 #### 引用[.reference_title] - *1* *2* *3* [unity 常用的几种解析json方式](https://blog.csdn.net/tianyongheng/article/details/78989612)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值