Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.

2,535 views
Skip to first unread message

Kevin Moss

unread,
Oct 23, 2012, 4:21:25 PM10/23/12
to rest...@googlegroups.com
Using RestSharp to read JSON Result from Hubspot API. The result comes back as a JSON array, but there is no root element defined.
 
This raises this error:
Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.
 
Here is a sample of the JSON that I get back in the response.content field:
[{"name":"salespartnerextension","label":"Sales Partner Extension","description":"","groupName":"contactinformation","type":"string","fieldType":"text","formField":true,"displayOrder":0,"options":[]},
{"name":"selectmanageprefs","label":"Select Manage Prefs","description":"","groupName":"contactinformation","type":"enumeration","fieldType":"checkbox","formField":true,"displayOrder":0,"options":[
 {"label":"Service Tips and Specials","value":"Service Tips and Specials","displayOrder":0},
 {"label":"Upcoming Events","value":"Upcoming Events","displayOrder":1},
 {"label":"betterRVing News","value":"betterRVing News","displayOrder":2},
 {"label":"New Products","value":"New Products","displayOrder":3},
 {"label":"Preowned Products and Specials","value":"Preowned Products and Specials","displayOrder":4},
 {"label":"Featured Products and Specials","value":"Featured Products and Specials","displayOrder":5}]
},....]
 
Any ideas on how to get around the error and map to an object? (I think my classes are correct).

John Sheehan

unread,
Oct 23, 2012, 4:40:14 PM10/23/12
to rest...@googlegroups.com
Post your code

Kevin Moss

unread,
Oct 23, 2012, 5:02:54 PM10/23/12
to rest...@googlegroups.com
 
OBJECT Defination:
 

public cass PropertyOptions
 {
   public string Label { get; set; }
   public string Value { get; set; }
   public int DisplayOrder { get; set; }
}
public class HSContactProperty
{
   public string Name { get; set; }
   public string Label { get; set; }
   public string Description { get; set; }
   public string GroupName { get; set; }
   public string Type { get; set; }
   public string FieldType { get; set; }
   public bool FormField { get; set; }
   public int DisplayOrder { get; set; }
   public List<PropertyOptions> Options { get; set; }
}
public class HSContactPropertyCollection
{
   public List<HSContactProperty> HSContactProperties { get; set; }
}
Method that calls API:
public HSContactPropertyCollection GetContactProperties()
{
   RestRequest request;
   HSContactPropertyCollection proplist;

   request = new RestRequest();
   request.Resource = "contacts/v1/properties";
   request.RequestFormat = DataFormat.Json;
   request.AddParameter(TokenType, ApiToken);
   proplist = Execute<HSContactPropertyCollection>(request);

   return proplist;
}

public T Execute<T>(RestRequest request) where T : new()
{
     RestClient client = new RestClient();
   client.BaseUrl = BaseUrl;           
   var response = client.Execute<T>(request);
   if (response.ErrorMessage != null && response.ErrorMessage.Length > 0) 
   {
      throw new Exception("Error contacting HubSpot: " + response.ErrorMessage, response.ErrorException);
   }
   if (response.Content.IndexOf("error",0) > 0)
   {
      throw new Exception("Error in Hubspot Routine:" + response.Content);
   }
   return response.Data;
}


n Moss wrote:

Kevin Moss

unread,
Oct 25, 2012, 10:59:15 AM10/25/12
to rest...@googlegroups.com
Issue has been resolved.
Had to use JSON.NET to build the following class which then allowed Restsharp to map the HSContactProperties. Not sure if there was a more elegent way of doing it, but hey it works.

    public class PropertyConverter : CustomCreationConverter<IContactProperty>
    {
        public override IContactProperty Create(Type objectType)
        {
            return new IContactProperty();
        }
    }

Reply all
Reply to author
Forward
0 new messages