Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting NullReferenceException when trying to deserialize JSON

so I'm trying to wrap my head around how to deserialize this properly.

{
  "_note": "You are currently authenticated with the API using your OPSkins login session cookies.",
  "status": 1,
  "time": 1500460072,
  "response": {
    "AK-47 | Aquamarine Revenge (Battle-Scarred)": {
      "op_7_day": 999,
      "op_30_day": 932
    },
    "AK-47 | Aquamarine Revenge (Factory New)": {
      "op_7_day": 2738,
      "op_30_day": 2665
    }
  }
}

Here is my class structure

public class OpRoot
{
    public string _note { get; set; }
    public int status { get; set; }
    public int time { get; set; }
    public OpResponse response { get; set; }
}

public class OpResponse
{
    public Dictionary<string, OpItem> items { get; set; }
}

public class OpItem
{
    public int op_7_day { get; set; }
    public int op_30_day { get; set; }
}

This is how I'm trying to deserialize it:

OpRoot OpInstance = JsonConvert.DeserializeObject<OpRoot>(readerResponse);

I have tried to change the Dictionary into a List instead, but got the same result "System.NullReferenceException" when trying to call the "items" object:

Console.WriteLine(OpInstance.response.items.Values);

So I think the problem lies within the code of the "OpResponse" class. Most of this code has worked before, however with another JSON structure.

Any help would be appreciated

EDIT: Fixed typo

like image 379
multikus Avatar asked Sep 09 '26 05:09

multikus


1 Answers

You don't need the OpResponse. With the following classes it should work:

public class OpRoot
{
   public string _note { get; set; }
   public int status { get; set; }
   public int time { get; set; }
   public Dictionary<string, OpItem> response { get; set; }
}

public class OpItem
{
   public int op_7_day { get; set; }
   public int op_30_day { get; set; }
}
like image 158
fknx Avatar answered Sep 10 '26 18:09

fknx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!