【问题标题】:JsonUtility.FromJson keeps throwing ArgumentExceptionJsonUtility.FromJson 不断抛出 ArgumentException
【发布时间】:2021-07-11 11:00:24
【问题描述】:

我正在尝试从我的统一项目中的后端接收数据,数据如下所示:

{"subjectid":98,"name":"test23","first_name":"test23","date_of_birth":"1998-02-16","age":23}

我正在使用以下行将数据放入对象中:

PatientBackend patient = JsonUtility.FromJson<PatientBackend>(responseBody);

对象如下所示:

  using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    [System.Serializable]
    public class PatientBackend : MonoBehaviour
    {
        public int subjectid;
        public string name;
        public string first_name;
        public string date_of_birth;
        public int age;
    
       public PatientBackend(string name, string first_name, string date_of_birth)
        {
            this.name = name;
            this.first_name = first_name;
            this.date_of_birth = date_of_birth;
        }
}

但是每次调用它都会抛出以下异常:

System.ArgumentException: Cannot deserialize JSON to new instances of type 'PatientBackend.'
  at UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) [0x00056] in <5070e0347dee4c9faba7201166fbed9d>:0 
  at UnityEngine.JsonUtility.FromJson[T] (System.String json) [0x00001] in <5070e0347dee4c9faba7201166fbed9d>:0 
  at DataService+<createPatient>d__8.MoveNext () [0x0021b] in C:\Users\diete\Documents\Stage\bedrijf_CLEAN-CLONE\VRStrokeRehabilitation\Unity\UnityProject\Assets\Scripts\DataService.cs:87 
UnityEngine.Debug:Log(Object)
<createPatient>d__8:MoveNext() (at Assets/Scripts/DataService.cs:90)

有人知道为什么这不起作用吗?

【问题讨论】:

  • 异常不是很清楚,但我认为这可能是因为PatientBackend 没有空的构造函数
  • 不,这似乎不是问题。我只是做了一个空的构造函数,并没有解决它。

标签: c# json unity3d exception argumentexception


【解决方案1】:

PatientBackend 不应该是 MonoBehaviour。 JsonUtility 只能用于数据类。 MonoBehaviours 是需要附加到游戏对象的组件。你不能简单地“创建”一个实例,这就是它失败的原因。

Answers.unity

【讨论】:

    【解决方案2】:

    您不能通过 JsonUtility 创建MonoBehaviour 的实例。 MonoBehaviour 仅在附加到 GameObject 时才有意义。

    MonoBehaviour 也不允许实现构造函数。


    要么根本不将其设为MonoBehaviour。而是使用

    [System.Serializable]
    public class PatientBackend
    {
        public int subjectid;
        public string name;
        public string first_name;
        public string date_of_birth;
        public int age;
        
        public PatientBackend(string name, string first_name, string date_of_birth)
        {
            this.name = name;
            this.first_name = first_name;
            this.date_of_birth = date_of_birth;
        }
    }
    

    var patientBackend = JsonUtility.FromJson<PatientBackend>(jsonString);
    

    或者,如果您确实需要 MonoBehaviour 而不是使用 JsonUtility.FromJsonOverwrite 以便仅覆盖现有实例的字段。

    public class PatientBackend : MonoBehaviour
    {
        public int subjectid;
        public string name;
        public string first_name;
        public string date_of_birth;
        public int age;
    }
    

    JsonUtility.FromJsonOverwrite(jsonString, existingpatientBackend);
    

    【讨论】:

    • 谢谢,我已经按照 Connor Stoop 所说的做了。我只是在没有让 patientBackend 成为单一行为的情况下做到了,它确实有效。
    猜你喜欢
    • 2012-08-10
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 2010-10-17
    • 2020-10-26
    相关资源
    最近更新 更多