-1

I am trying to deserialize a JSON http response, but I keep getting the error "object reference not set to an instance of an object".

I know this is a commonly asked question, but nothing I found about the error has helped me. I think I am missing something very basic.

The JSON response I'm getting looks something like this:

{
 "value": [
        {
            "createdDateTime": "2020-03-14T12:25:54Z",
            "id": "W2RQ6DOBCJUXWGR35DLKP7UI6IN",
            "name": "somename",
            "size": 200230,
        },
        {
            "createdDateTime": "2020-03-24T08:25:03Z",
            "id": "Q6DOBCJUXWFIGR35DLWERKP7UI6IN",
            "name": "somename",
            "size": 200230,
        },

        ...
   ]
}

I already managed to deserialize properties that only occur once in the response. However, this time I need the properties "id" and "name" back. Here is how I tried that:

 public class Value
        {
            public string Name { get; set; }
            public string Id { get; set; }
            public Value[] Children { get; set; }
        }
        public class RootObject
        {
            public Value[] RespValues { get; set; }
        }

var objJSON = JObject.Parse(jsonresponse);
var objResponse = objJSON["Response"].ToString();

RootObject categoryTree = JsonConvert.DeserializeObject<RootObject>(objResponse);

I tried a few different approaches so far, but every single one caused the error. I'm not really sure what I'm doing wrong.

e_mai
  • 51
  • 2
  • 7
  • 1
    Does this answer your question? [How can I change property names when serializing with Json.net?](https://stackoverflow.com/questions/8796618/how-can-i-change-property-names-when-serializing-with-json-net) – madreflection Apr 02 '20 at 23:57
  • No, I don't really need to change the property names and I'm not sure if that has something to do with the error I'm getting at all. – e_mai Apr 03 '20 at 00:01
  • Are you sure? Try putting `[JsonProperty("value")]` on `RespValues`. – madreflection Apr 03 '20 at 00:11
  • I tried it, but I'm still getting the same error – e_mai Apr 03 '20 at 00:16
  • 1
    You have a comma after the `"size": 200230,` property. It cannot be there. Your `Value` class is missing two properties and this one: `public Value[] Children { get; set; }` doesn't exist. `size` should be of type `long` and `createdDateTime` of type DateTimeOffset. – Jimi Apr 03 '20 at 03:55

1 Answers1

1

In this line var objResponse = objJSON["Response"].ToString(); you try to get "Response" value from your json, but you dont have any field named "Response"

Also in your RootObject class, you have RespValue property, but your json is "value" you should make this 2 field have same name. either you change your class's property to @value or your json to "respValue"

then, if you want to parse that json, you can use this one

var objJSON = JsonConvert.DeserializeObject<RootObject>(json);

to access its value, you can use array index or foreach loop from yout objJSON variable

Here's my full code based on your question

public class Value
{
    public string Name { get; set; }
    public string Id { get; set; }
    public Value[] Children { get; set; } // will always null
}
public class RootObject
{
    public Value[] @value { get; set; } // Your json's field named value
}

public static void Main()
{
    string jsonexample = @"{""value"": [
                                        {
                                            ""createdDateTime"": ""2020-03-14T12:25:54Z"",
                                            ""id"": ""W2RQ6DOBCJUXWGR35DLKP7UI6IN"",
                                            ""name"": ""somename"",
                                            ""size"": 200230,
                                        },
                                        {
                                            ""createdDateTime"": ""2020-03-24T08:25:03Z"",
                                            ""id"": ""Q6DOBCJUXWFIGR35DLWERKP7UI6IN"",
                                            ""name"": ""somename"",
                                            ""size"": 200230,
                                        }
                                   ]
                                }";
    var objJSON = JsonConvert.DeserializeObject<RootObject>(jsonexample);

    Console.WriteLine(objJSON.value[0].Name);

    foreach(var item in objJSON.value)
    {
        Console.WriteLine(item.Id);
    }
}
hphp
  • 2,142
  • 2
  • 13
  • 26