Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize JSON using restsharp

I am trying to deserialize data from Harvest but its failing (no error): https://github.com/harvesthq/api#api-json

The data returned looks like this:

Updated (see at the bottom for complete JSON response)

The output when i run the code below is a list with x posts where each post contains an id = 0

Is there a setting or something that i have missed to make it ignore/parse the surrounding []?

[DeserializeAs(Name = "project")]
public class Project
{
    public int id { get; set; }

    //public string name { get; set; }

    //[DeserializeAs(Name = "created-at")]
    //public DateTime CreatedAt { get; set; }           
}

// The following is the methods to request for testing

public List<Project> GetProjects()
{
    var request = new RestRequest("projects", Method.GET);
    request.RequestFormat = DataFormat.Json;
    return Execute<List<Project>>(request);
}

private T Execute<T>(RestRequest request) where T : new()
{
    var client = new RestClient();            
    client.BaseUrl = BaseUrl;
    client.Authenticator = new HttpBasicAuthenticator(_username, _password);
    var response = client.Execute<T>(request);

    if (response.ErrorException != null)
    {
        const string message = "Error retrieving response.  Check inner details for more info.";
        var exception = new ApplicationException(message, response.ErrorException);
        throw exception;
    }
    return response.Data;
}

Data returned from Harvest:

[
  {
    "project": {
      "id": 123456,
      "client_id": 219854,
      "name": "Test proj 1",
      "code": "",
      "active": false,
      "billable": true,
      "bill_by": "Tasks",
      "cost_budget": null,
      "cost_budget_include_expenses": false,
      "hourly_rate": null,
      "budget": 8,
      "budget_by": "project",
      "notify_when_over_budget": false,
      "over_budget_notification_percentage": 80,
      "over_budget_notified_at": null,
      "show_budget_to_all": false,
      "created_at": "2014-04-03T09:49:00Z",
      "updated_at": "2014-07-02T11:45:07Z",
      "estimate": 8,
      "estimate_by": "project",
      "hint_earliest_record_at": "2014-04-03",
      "hint_latest_record_at": "2014-04-03",
      "notes": ""
    }
  },
  {
    "project": {
      "id": 234567,
      "client_id": 686547,
      "name": "Test porj 2",
      "code": "",
      "active": true,
      "billable": true,
      "bill_by": "Tasks",
      "cost_budget": null,
      "cost_budget_include_expenses": false,
      "hourly_rate": null,
      "budget": 8,
      "budget_by": "project",
      "notify_when_over_budget": false,
      "over_budget_notification_percentage": 80,
      "over_budget_notified_at": null,
      "show_budget_to_all": false,
      "created_at": "2014-04-03T09:48:28Z",
      "updated_at": "2014-04-15T20:47:29Z",
      "estimate": 8,
      "estimate_by": "project",
      "hint_earliest_record_at": "2014-04-03",
      "hint_latest_record_at": "2014-04-03",
      "notes": ""
    }
  },
  {
    "project": {
      "id": 345678,
      "client_id": 987456,
      "name": "Test proj 3",
      "code": "",
      "active": false,
      "billable": true,
      "bill_by": "Project",
      "cost_budget": null,
      "cost_budget_include_expenses": false,
      "hourly_rate": null,
      "budget": 8,
      "budget_by": "project",
      "notify_when_over_budget": false,
      "over_budget_notification_percentage": 80,
      "over_budget_notified_at": null,
      "show_budget_to_all": false,
      "created_at": "2013-04-26T13:21:35Z",
      "updated_at": "2014-03-30T18:05:24Z",
      "estimate": 8,
      "estimate_by": "project",
      "hint_earliest_record_at": "2013-04-26",
      "hint_latest_record_at": "2013-12-04",
      "notes": "Scriblings from meeting ..."
    }
  }
]
like image 661
Patrick Avatar asked Jul 17 '14 20:07

Patrick


People also ask

Does RestSharp use Newtonsoft?

Newtonsoft. Json package is not provided by RestSharp, is marked as obsolete on NuGet, and no longer supported by its creator.

What is RestSharp?

RestSharp is a C# library used to build and send API requests, and interpret the responses. It is used as part of the C#Bot API testing framework to build the requests, send them to the server, and interpret the responses so assertions can be made.

What does Jsonconvert Deserializeobject do?

Deserializes the JSON to the specified . NET type. Deserializes the JSON to the specified . NET type using a collection of JsonConverter.


2 Answers

You're misinterpreting the JSON response.

[
  {
    "project": {
      "id": 123456
    }
  },
  {
    "project": {
      "id": 234567
    }
  }
]

That's an array with objects that contain a Project. I'm not familiar with RestSharp, but something like this should do:

public class SomeType
{
    public Project project { get; set; }
}
return Execute<List<SomeType>>(request);

You might want to get in touch with the project maintainers though, according to the documentation /projects should return an array of projects.

like image 191
user247702 Avatar answered Sep 30 '22 16:09

user247702


As Stijn mentioned your service is returning an array of a container object with a property of the type project.

I took the liberty to recreate your code sample with the fix suggested by Stijn see it below.

using RestSharp;
using RestSharp.Deserializers;
using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();

            var project = p.GetProjects();
        }

        public class ProjectResult 
        {
            public Project project { get; set; }
        }

        public class Project
        {
            public int id { get; set; }
        }

        public List<ProjectResult> GetProjects()
        {
            var request = new RestRequest("projects", Method.GET);
            request.RequestFormat = DataFormat.Json;
            return Execute<List<ProjectResult>>(request);
        }

        private T Execute<T>(RestRequest request) where T : new()
        {
            var client = new RestClient();
            client.BaseUrl = "http://127.0.0.1:1337/";
            //client.Authenticator = new HttpBasicAuthenticator(_username, _password);
            var response = client.Execute<T>(request);

            if (response.ErrorException != null)
            {
                const string message = "Error retrieving response.  Check inner details for more info.";
                var exception = new ApplicationException(message, response.ErrorException);
                throw exception;
            }
            return response.Data;
        }
    }
}
like image 45
dmportella Avatar answered Sep 30 '22 15:09

dmportella