0

I'm trying to deserialize a Json string.

This is my code:

[System.Serializable]
public class SharedWorlds
{
    public int worldId { get; set; }
    public System.DateTime uploaded { get; set; }
    public string username { get; set; }
    public string levelName { get; set; }
    public string gameVersion { get; set; }
    public string description { get; set; }
    public string filename { get; set; }
    public string screenshot1 { get; set; }
    public string screenshot2 { get; set; }
    public string userTag { get; set; }
    public string userURL { get; set; }
    public double price { get; set; }
    public int    nrDownload { get; set; }
    public int    votes { get; set; }
}
[System.Serializable]
public class Record {
    public List<SharedWorlds> record;
}

    try {
        SDE3D _webService = new SDE3D();
        result= _webService.GetMassiveWorldsList ();

        var records = JsonUtility.FromJson<Record>(result);
    }
    catch(System.Exception ex) {
        Debug.Log (ex.Message.ToString ());
    }

And this is my valid jSon (here two records, but I want to send many records per time).

[
  {
    "worldId": 5,
    "uploaded": "/Date(1524875719000)/",
    "username": "quik",
    "levelName": "Station",
    "gameVersion": "1.0.1",
    "description": "iwoeijksf",
    "filename": "0000003.dat",
    "screenshot1": "0000003a.png",
    "screenshot2": "0000003b.png",
    "userTag": "",
    "userURL": "",
    "price": 0,
    "nrDownload": 5,
    "votes": 5
  },
  {
    "worldId": 4,
    "uploaded": "/Date(1524875659000)/",
    "username": "aksio",
    "levelName": "Garage",
    "gameVersion": "1.0.1",
    "description": "Adlkld",
    "filename": "0000003.dat",
    "screenshot1": "0000003a.png",
    "screenshot2": "0000003b.png",
    "userTag": "",
    "userURL": "",
    "price": 0,
    "nrDownload": 4,
    "votes": 4
  }  
]

I'm getting error:

"ArgumentException: JSON must represent an object type."

I'm pretty sure the error is in this code line:

var records = JsonUtility.FromJson<Record>(result);

How to deserialize an array of json object ?

Thanks

Programmer
  • 121,791
  • 22
  • 236
  • 328
stighy
  • 7,260
  • 25
  • 97
  • 157
  • I guess the JSON has no `record` field, which your `Record` class does. I think that, for that line to work, `Record` would have to BE a `List` rather than contain one. – jmcilhinney Apr 28 '18 at 11:48
  • https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity/36244111#36244111 – vadzim dvorak Apr 28 '18 at 11:49
  • Try using `JsonUtility.FromJson(result);` – Markiian Benovskyi Apr 28 '18 at 11:49
  • 4
    Why is that class called `SharedWorlds`, though, and not `SharedWorld`? Surely each one of those is just _one_ world? – Nyerguds Apr 28 '18 at 11:51
  • 1
    Your json don't match with the object you are trying to de-serialize it into. There are other issues such as using a property: `{ get; set; }` in that object and also attempting to use `JsonUtility.FromJson` to de-serialize json array.This won't work(Not supported). I was about to put an answer but I have answered everything covering your issue before and think this is better marked as a duplicate. First, use [this](http://json2csharp.com/) to generate your data from the json array. Remove the `{ get; set; }` then use `JsonHelper.FromJson` to de-serialize your json array. – Programmer Apr 28 '18 at 12:29

2 Answers2

3

Because your JSON data is not a Record. It's a collection of SharedWorlds. So something like this:

var sharedWorlds = JsonUtility.FromJson<SharedWorld[]>(result);

Or perhaps:

var sharedWorlds = JsonUtility.FromJson<List<SharedWorld>>(result);

From which you could create a Record:

var record = new Record { record = sharedWorlds };

If the JSON needs to deserialize into a Record then it would need to be in the format of a Record object:

{
    "record": 
    [
        /* the rest of your JSON within the square brackets */
    ]
}

Then it would be a Record:

var record = JsonUtility.FromJson<Record>(result);

*Side note: Your class and variable names and the pluralizations you're using are really confusing. The semantics of which is probably not making your debugging any easier for you.

David
  • 208,112
  • 36
  • 198
  • 279
-2

You get to script collection of object no one single object in your JSON

kalit
  • 256
  • 4
  • 15