简体   繁体   中英

Unity3D - Cannot deserialize JSON to object

I have this JSON response:

{
  "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:

[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:

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?

What have I done wrong with my deserialization? :-)

The problem is that your TrainJsonResponse extends from MonoBehaviour and JsonUtility can not deserialize a 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.

Warning: The JSON Serializer API supports MonoBehaviour and ScriptableObject subclasses as well as plain structs and classes. However, when deserializing JSON into subclasses of MonoBehaviour or ScriptableObject, you must use the FromJsonOverwrite method. If you try to use FromJson, Unity throws an exception because this behavior is not supported. JSON Serialization

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM