[英]Unity3D - Cannot deserialize JSON to object
I have this JSON response:我有这个 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
}
I am trying to convert into an object, using the following classes:我正在尝试使用以下类转换为 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;
}
Using the JsonUtility.FromJson
method:使用
JsonUtility.FromJson
方法:
TrainJsonResponse trainData = JsonUtility.FromJson<TrainJsonResponse>(jsonResponse);
But when I try doing this I get an error:但是当我尝试这样做时,我得到一个错误:
ArgumentException: Cannot deserialize JSON to new instances of type 'TrainJsonResponse.'
As I understand, the fact that I am only using two to three of the fields from the JSON response shouldn't matter, Unity should skip/ignore these fields and map what it can?据我了解,我只使用 JSON 响应中的两到三个字段这一事实无关紧要,Unity 应该跳过/忽略这些字段和 map 它可以做什么?
What have I done wrong with my deserialization?我的反序列化做错了什么? :-)
:-)
The problem is that your TrainJsonResponse
extends from MonoBehaviour
and JsonUtility
can not deserialize a MonoBehaviour
.问题是您的
TrainJsonResponse
从MonoBehaviour
扩展,而JsonUtility
无法反序列化MonoBehaviour
。 You should change it to this:你应该把它改成这样:
[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;
}
In case of objects that inherited from MonoBehaviour, when you deserialize it you must use the JsonUtility.FromJsonOverwrite vs JsonUtility.FromJson, because the "JsonUtility.FromJsonOverwrite" does not try to create a new MonoBehaviour instance by the constructor what is not allowed.对于从 MonoBehaviour 继承的对象,当您反序列化它时,您必须使用 JsonUtility.FromJsonOverwrite 与 JsonUtility.FromJson,因为“JsonUtility.FromJsonOverwrite”不会尝试由构造函数创建新的 MonoBehaviour 实例,这是不允许的。
Warning: The JSON Serializer API supports MonoBehaviour and ScriptableObject subclasses as well as plain structs and classes.
警告:JSON 序列化器 API 支持 MonoBehaviour 和 ScriptableObject 子类以及普通结构和类。 However, when deserializing JSON into subclasses of MonoBehaviour or ScriptableObject, you must use the FromJsonOverwrite method.
但是,在将 JSON 反序列化为 MonoBehaviour 或 ScriptableObject 的子类时,必须使用 FromJsonOverwrite 方法。 If you try to use FromJson, Unity throws an exception because this behavior is not supported.
如果您尝试使用 FromJson,Unity 会抛出异常,因为此行为不受支持。 JSON Serialization
JSON 序列化
I had a similar problem, but it was because the class I was deserializing to was marked as abstract
, removed the abstract
keyword and it worked fine.我有一个类似的问题,但这是因为我反序列化到的 class 被标记为
abstract
,删除了abstract
关键字并且它工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.