![](/img/trans.png)
[英]"NullReferenceException" in Unity3D when trying deserialize data from JSON
[英]Unity3D - Cannot deserialize JSON to object
我有这个 JSON 响应:
{
"trainServices": [
{
"origin": [
{
"locationName": "Somewhere",
"crs": "smw",
"via": null,
"futureChangeTo": null,
"assocIsCancelled": false
}
],
"destination": [
{
"locationName": "Somewhere",
"crs": "smw",
"via": null,
"futureChangeTo": null,
"assocIsCancelled": false
}
],
"currentOrigins": null,
"currentDestinations": null,
"rsid": "NT208200",
"sta": null,
"eta": null,
"std": "21:07",
"etd": "21:10",
"platform": "1",
"operator": "Northern",
"operatorCode": "NT",
"isCircularRoute": false,
"isCancelled": false,
"filterLocationCancelled": false,
"serviceType": 0,
"length": 2,
"detachFront": false,
"isReverseFormation": false,
"cancelReason": null,
"delayReason": null,
"adhocAlerts": null
},
{
"origin": [
{
"locationName": "Somewhere",
"crs": "smw",
"via": null,
"futureChangeTo": null,
"assocIsCancelled": false
}
],
"destination": [
{
"locationName": "Somewhere",
"crs": "smw",
"via": null,
"futureChangeTo": null,
"assocIsCancelled": false
}
],
"currentOrigins": null,
"currentDestinations": null,
"rsid": "NT224200",
"sta": null,
"eta": null,
"std": "21:42",
"etd": "On time",
"platform": "2",
"operator": "Northern",
"operatorCode": "NT",
"isCircularRoute": false,
"isCancelled": false,
"filterLocationCancelled": false,
"serviceType": 0,
"length": 2,
"detachFront": false,
"isReverseFormation": false,
"cancelReason": null,
"delayReason": null,
"adhocAlerts": null
},
{
"origin": [
{
"locationName": "Somewhere",
"crs": "smw",
"via": null,
"futureChangeTo": null,
"assocIsCancelled": false
}
],
"destination": [
{
"locationName": "Somewhere",
"crs": "smw",
"via": null,
"futureChangeTo": null,
"assocIsCancelled": false
}
],
"currentOrigins": null,
"currentDestinations": null,
"rsid": "NT208300",
"sta": null,
"eta": null,
"std": "22:07",
"etd": "On time",
"platform": "1",
"operator": "Northern",
"operatorCode": "NT",
"isCircularRoute": false,
"isCancelled": false,
"filterLocationCancelled": false,
"serviceType": 0,
"length": 3,
"detachFront": false,
"isReverseFormation": false,
"cancelReason": null,
"delayReason": null,
"adhocAlerts": null
},
{
"origin": [
{
"locationName": "Somewhere",
"crs": "smw",
"via": null,
"futureChangeTo": null,
"assocIsCancelled": false
}
],
"destination": [
{
"locationName": "Somewhere",
"crs": "smw",
"via": null,
"futureChangeTo": null,
"assocIsCancelled": false
}
],
"currentOrigins": null,
"currentDestinations": null,
"rsid": "NT224700",
"sta": null,
"eta": null,
"std": "22:52",
"etd": "On time",
"platform": "2",
"operator": "Northern",
"operatorCode": "NT",
"isCircularRoute": false,
"isCancelled": false,
"filterLocationCancelled": false,
"serviceType": 0,
"length": 3,
"detachFront": false,
"isReverseFormation": false,
"cancelReason": null,
"delayReason": null,
"adhocAlerts": null
}
],
"busServices": null,
"ferryServices": null,
"generatedAt": "2019-10-01T20:03:23.2126313+00:00",
"locationName": "Somewhere",
"crs": "smw",
"filterLocationName": null,
"filtercrs": null,
"filterType": 0,
"nrccMessages": null,
"platformAvailable": true,
"areServicesAvailable": true
}
我正在尝试使用以下类转换为 object:
[System.Serializable]
public class TrainJsonResponse : MonoBehaviour
{
public TrainServices[] trainServices;
}
[System.Serializable]
public class TrainServices
{
public Origin[] origin;
public string std;
public string etd;
}
[System.Serializable]
public class Origin
{
public string locationName;
}
使用JsonUtility.FromJson
方法:
TrainJsonResponse trainData = JsonUtility.FromJson<TrainJsonResponse>(jsonResponse);
但是当我尝试这样做时,我得到一个错误:
ArgumentException: Cannot deserialize JSON to new instances of type 'TrainJsonResponse.'
据我了解,我只使用 JSON 响应中的两到三个字段这一事实无关紧要,Unity 应该跳过/忽略这些字段和 map 它可以做什么?
我的反序列化做错了什么? :-)
问题是您的TrainJsonResponse
从MonoBehaviour
扩展,而JsonUtility
无法反序列化MonoBehaviour
。 你应该把它改成这样:
[System.Serializable]
public class TrainJsonResponse
{
public TrainServices[] trainServices;
}
[System.Serializable]
public class TrainServices
{
public Origin[] origin;
public string std;
public string etd;
}
[System.Serializable]
public class Origin
{
public string locationName;
}
对于从 MonoBehaviour 继承的对象,当您反序列化它时,您必须使用 JsonUtility.FromJsonOverwrite 与 JsonUtility.FromJson,因为“JsonUtility.FromJsonOverwrite”不会尝试由构造函数创建新的 MonoBehaviour 实例,这是不允许的。
警告:JSON 序列化器 API 支持 MonoBehaviour 和 ScriptableObject 子类以及普通结构和类。 但是,在将 JSON 反序列化为 MonoBehaviour 或 ScriptableObject 的子类时,必须使用 FromJsonOverwrite 方法。 如果您尝试使用 FromJson,Unity 会抛出异常,因为此行为不受支持。 JSON 序列化
我有一个类似的问题,但这是因为我反序列化到的 class 被标记为abstract
,删除了abstract
关键字并且它工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.