Search code examples
arraysvb.netjson.netjson-deserialization

(JSON.net) cannot deserialize the current JSON array, it comes from a web-api but is it wrong?


(using JSON.net) I'm trying to parse an array generated by a web-api (website). This is the output:

[
{
    "difficulty": 100,
    "size": 8,
    "kill": 1,
    "name": "Refurbisher 0",
    "specs": [
        {
            "class": "Global",
            "spec": "Scholar",
            "combined": false,
            "data": [
                {
                    "character_id": 138634,
                    "character_name": "---",
                    "persecondamount": 1162.87,
                    "ilvl": 3.4,
                    "duration": 369801,
                    "start_time": 1476819363468,
                    "report_code": "GP4w1NgWjVF87dYH",
                    "report_fight": 5,
                    "ranking_id": 113148996,
                    "guild": null,
                    "total": 0,
                    "rank": "-",
                    "percent": 89.939300240724,
                    "exploit": 0,
                    "banned": false,
                    "historical_count": 2240,
                    "historical_percent": 91
                },
                {
                    "character_id": 138634,
                    "character_name": "---",
                    "persecondamount": 1103.58,
                    "ilvl": 3.4,
                    "duration": 329641,
                    "start_time": 1477420365118,
                    "report_code": "qtmKzMnFx6NXy1VR",
                    "report_fight": 4,
                    "ranking_id": 118427755,
                    "guild": null,
                    "total": 0,
                    "rank": "-",
                    "percent": 86.86332696937,
                    "exploit": 0,
                    "banned": false,
                    "historical_count": 2601,
                    "historical_percent": 86
                },
                {
                    "character_id": 138634,
                    "character_name": "---",
                    "persecondamount": 775.108,
                    "ilvl": 3.4,
                    "duration": 397106,
                    "start_time": 1475608153904,
                    "report_code": "Q9rGtyaLv3fCM28K",
                    "report_fight": 14,
                    "ranking_id": 107727522,
                    "guild": null,
                    "total": 0,
                    "rank": "-",
                    "percent": 60.798351443404,
                    "exploit": 0,
                    "banned": false,
                    "historical_count": 1097,
                    "historical_percent": 71
                },
                {
                    "character_id": 138634,
                    "character_name": "---",
                    "persecondamount": 774.709,
                    "ilvl": 3.4,
                    "duration": 383086,
                    "start_time": 1476212953820,
                    "report_code": "a98jMzhDHYbmdVTt",
                    "report_fight": 9,
                    "ranking_id": 110419488,
                    "guild": null,
                    "total": 0,
                    "rank": "-",
                    "percent": 60.757687862686,
                    "exploit": 0,
                    "banned": false,
                    "historical_count": 1762,
                    "historical_percent": 67
                },
                {
                    "character_id": 138634,
                    "character_name": "---",
                    "persecondamount": 571.689,
                    "ilvl": 3.4,
                    "duration": 455377,
                    "start_time": 1475086407115,
                    "report_code": "XbFYWyL1gDJ9pB4Q",
                    "report_fight": 3,
                    "ranking_id": 106604925,
                    "guild": null,
                    "total": 0,
                    "rank": "-",
                    "percent": 39.931594034917,
                    "exploit": 0,
                    "banned": false,
                    "historical_count": 209,
                    "historical_percent": 43
                }
            ],
            "best_persecondamount": 1162.87,
            "best_duration": 329641,
            "best_historical_percent": 91,
            "best_allstar_points": 102.78152640182,
            "best_combined_allstar_points": 0,
            "possible_allstar_points": 120,
            "historical_total": 5,
            "historical_median": 71,
            "historical_avg": 71.6
        }
    ],
    "variable": false,
    "partition": 1
}
]

But I keep getting this error: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'WindowsApplication1.FFLogs' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

Currently using this piece of code (replace json with above):

Dim obj = JsonConvert.DeserializeObject(Of FFLogs)(json)
MessageBox.Show(obj.name)

My classes:

Public Class Datum
    Public Property character_id As Integer
    Public Property character_name As String
    Public Property persecondamount As Double
    Public Property ilvl As Double
    Public Property duration As Integer
    Public Property start_time As Object
    Public Property report_code As String
    Public Property report_fight As Integer
    Public Property ranking_id As Integer
    Public Property guild As Object
    Public Property total As Integer
    Public Property rank As String
    Public Property percent As Double
    Public Property exploit As Integer
    Public Property banned As Boolean
    Public Property historical_count As Integer
    Public Property historical_percent As Integer
End Class

Public Class Spec
    Public Property spec As String
    Public Property combined As Boolean
    Public Property data As Datum()
    Public Property best_persecondamount As Double
    Public Property best_duration As Integer
    Public Property best_historical_percent As Integer
    Public Property best_allstar_points As Double
    Public Property best_combined_allstar_points As Integer
    Public Property possible_allstar_points As Integer
    Public Property historical_total As Integer
    Public Property historical_median As Double
    Public Property historical_avg As Double
End Class

Public Class FFLogs
    Public Property difficulty As Integer
    Public Property size As Integer
    Public Property kill As Integer
    Public Property name As String
    Public Property specs As Spec()
    Public Property variable As Boolean
    Public Property partition As Integer
End Class

Any tip on how I could make this work? I don't know if I have to use another way of deserializing because of the output being weird or smth, tried making the output a one liner too to see if that'd help.


Solution

  • Try changing your code to this:

    Dim obj = JsonConvert.DeserializeObject(Of List(Of FFLogs))(json)
    MessageBox.Show(obj.name)