【发布时间】: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