【发布时间】:2019-10-01 20:10:55
【问题描述】:
我有这个 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
}
我正在尝试使用以下类转换为对象:
[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 应该跳过/忽略这些字段并映射它可以映射的内容?
我的反序列化做错了什么? :-)
【问题讨论】: