【问题标题】:Unity3D - Cannot deserialize JSON to objectUnity3D - 无法将 JSON 反序列化为对象
【发布时间】: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 应该跳过/忽略这些字段并映射它可以映射的内容?

我的反序列化做错了什么? :-)

【问题讨论】:

    标签: c# json unity3d


    【解决方案1】:

    问题是您的TrainJsonResponse 扩展自MonoBehaviourJsonUtility 无法反序列化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;
    }
    

    【讨论】:

    • 完美!谢谢:-)
    • 不客气 :)
    【解决方案2】:

    对于继承自 MonoBehaviour 的对象,当您反序列化它时,您必须使用 JsonUtility.FromJsonOverwrite 与 JsonUtility.FromJson,因为“JsonUtility.FromJsonOverwrite”不会尝试由构造函数创建新的 MonoBehaviour 实例。允许。

    警告:JSON 序列化程序 API 支持 MonoBehaviour 和 ScriptableObject 子类以及普通结构和类。但是,在将 JSON 反序列化为 MonoBehaviour 或 ScriptableObject 的子类时,必须使用 FromJsonOverwrite 方法。如果您尝试使用 FromJson,Unity 会抛出异常,因为此行为不受支持。 JSON Serialization

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 2022-07-05
      • 2016-12-30
      • 2014-08-04
      • 1970-01-01
      相关资源
      最近更新 更多