1

I have a JSON string that I am getting from the BaseCamp API. I know for a fact that the JSON is valid, however, I am not able to DeserializeObject using Newtonsoft.Json.

I get an error saying:

Cannot deserialize the current JSON array (e.g.[1,2,3]) into type BaseCamp.Code.Projects+RootObject because the type requires a JSON objet (e.g. {"name":"value"}) to deserialize correctly.

The JSON (Unformated What is returned from the API Minus the URL's Values)

[
    {
        "id":6656986,
        "name":"Physics Revamp",
        "description":"ISU department of physics website redesign",
        "archived":false,
        "is_client_project":true,
        "created_at":"2014-08-07T10:59:29.000-05:00",
        "updated_at":"2014-10-30T09:18:01.000-05:00",
        "trashed":false,
        "color":"2c5322",
        "draft":false,
        "template":false,
        "last_event_at":"2014-10-30T09:18:01.000-05:00",
        "starred":false,
        "url":"xxxxxxxxxxxxxxxxxxxxxxx",
        "app_url":"xxxxxxxxxxxxxxx"
    },
    {
        "id":7178664,
        "name":"Physics Videos",
        "description":"",
        "archived":false,
        "is_client_project":false,
        "created_at":"2014-10-02T08:34:46.000-05:00",
        "updated_at":"2014-10-23T08:40:17.000-05:00",
        "trashed":false,
        "color":"660099",
        "draft":false,
        "template":false,
        "last_event_at":"2014-10-23T08:40:17.000-05:00",
        "starred":false,
        "url":"xxxxxxxxxxxxxxxxxxxxxxx",
        "app_url":"xxxxxxxxxxxxxxxxxxx"
    },
    {
        "id":6685451,
        "name":"WZND Website 2014",
        "description":"",
        "archived":false,
        "is_client_project":true,
        "created_at":"2014-08-11T13:25:51.000-05:00",
        "updated_at":"2014-10-30T11:26:39.000-05:00",
        "trashed":false,
        "color":"3185c5",
        "draft":false,
        "template":false,
        "last_event_at":"2014-10-30T11:26:39.000-05:00",
        "starred":false,
        "url":"xxxxxxxxxxxxxxxxxx",
        "app_url":"xxxxxxxxxxxxxxxxx"
    }
]

My C# class:

public class Projects  
    {  
        public class RootObject  
        {  
            public int id { get; set; }  
            public string name { get; set; }  
            public string description { get; set; }  
            public bool archived { get; set; }  
            public bool is_client_project { get; set; }  
            public string created_at { get; set; }  
            public string updated_at { get; set; }  
            public bool trashed { get; set; }  
            public string color { get; set; }  
            public bool draft { get; set; }  
            public bool template { get; set; }  
            public string last_event_at { get; set; }  
            public bool starred { get; set; }  
            public string url { get; set; }  
            public string app_url { get; set; }  
        }  
    }    

I am assuming something is wrong with the way my class is set up, but I can't see it.

3
  • 2
    How are you trying to deserialize your json? This seems to be a list of multiple RootObjects, so you have to make sure you're deserializing it into a list or array and not into a single RootObject. Commented Oct 30, 2014 at 19:11
  • var json = JsonConvert.DeserializeObject<Projects.RootObject>(response); This has worked for me. In fact I just used it earlier in the app. response is the json string that I get from the api. Commented Oct 30, 2014 at 19:13
  • 1
    This works if the json string only contains 1 RootObject, but in the given case it contains two. Therefor you need to deserialize it into a collection of RootObjects Commented Oct 30, 2014 at 19:18

1 Answer 1

2

You need to convert to an array of RootObject:

var json = JsonConvert.DeserializeObject<Projects.RootObject[]>(response); 

or list (or any other collection you want for that matter)...

var json = JsonConvert.DeserializeObject<List<Projects.RootObject>>(response); 
Sign up to request clarification or add additional context in comments.

4 Comments

After doing that then how do I call the different values? Before I would just do json.name or json.id. Which doesn't seem to be working anymore with this.
You can use foreach (var item in json) to access all items one by one, or json[0] to access first item, json[1] to access second item and so on...
So then what is the diffence between public class RootObject { public int expires_in { get; set; } public string access_token { get; set; } public string refresh_token { get; set; } } and the one I have up there? I was able to use my old method for it.
Your class is ONE object. The json var above is a list/array with MANY objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.