0

I'm trying to use RestSharp to get data from a REST web service. The service is returning data and I can see the JSON in the content string of the RestResponse object but no matter how many rows I insert into the database, the Data array of the RestResponse object only contains one entry and all of the fields are null.

Here is the Notification object

public class Notification
{
    [JsonProperty("message")]
    public string MessageText { get; set; }
    [JsonProperty("received")]
    public string NotificationDateTime { get; set; }
    [JsonProperty("effective")]
    public string EffectiveDateTime { get; set; }
    [JsonProperty("expiration")]
    public string ExpirationDateTime { get; set; }
    [JsonProperty("displayIsp")]
    public string DisplayAtIsp { get; set; }
    [JsonProperty("displayPos")]
    public string DisplayAtPos { get; set; }
    [JsonProperty("displayBow")]
    public string DisplayAtBow { get; set; }
    [JsonProperty("displayHandheld")]
    public string DisplayAtMobile { get; set; }
}

The web service call

        try
        {
            var restClient = new RestClient(new Uri(serviceUrl)) {ThrowOnAnyError = true};
            var restRequest = new RestRequest($"/{_companyCode}/{_storeNumber}/", Method.GET);
            var notifications = restClient.Execute<List<Notification>>(restRequest);

            Console.WriteLine();

        }
        catch (Exception exception)
        {
            Console.WriteLine( exception.Message );
        }

JSON data

{
    "notifications": [
        {
            "message": "test notification 09/29 17:39:49",
            "received": "09-29-2020 17:39:49",
            "effective": null,
            "expiration": null,
            "displayIsp": null,
            "displayPos": null,
            "displayBow": null,
            "displayHandheld": null
        },
        {
            "message": "test notification 09/29 17:39:49",
            "received": "09-29-2020 17:39:49",
            "effective": null,
            "expiration": null,
            "displayIsp": null,
            "displayPos": null,
            "displayBow": null,
            "displayHandheld": null
        }
    ],
    "companyNbr": 2,
    "storeNbr": 988
}

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
  • Does this answer your question? [Deserializing a json string with newtonsoft or restsharp](https://stackoverflow.com/questions/16530060/deserializing-a-json-string-with-newtonsoft-or-restsharp) – ndogac Sep 29 '20 at 21:19
  • It's been a while since I worked with RestSharp, but I would speculate that the problem is that it doesn't know how to translate the contents of `Content` into `Notification` objects: there's no obvious link between the "notifications" tag and the `Notification` object. – Ann L. Sep 29 '20 at 21:20
  • What does the JSON look like? – Jonathan Sep 29 '20 at 21:31
  • JSON data = {"notifications":[{"message":"test notification 09/29 17:39:49","received":"09-29-2020 17:39:49","effective":null,"expiration":null,"displayIsp":null,"displayPos":null,"displayBow":null,"displayHandheld":null},{"message":"test notification 09/29 17:39:49","received":"09-29-2020 17:39:49","effective":null,"expiration":null,"displayIsp":null,"displayPos":null,"displayBow":null,"displayHandheld":null}],"companyNbr":2,"storeNbr":988} –  Sep 29 '20 at 21:40
  • following the link that N.Dogac posted, I changed to use JsonConvert .... to deserialize a single instance of the notification. But the deserialization still returned null for all fields –  Sep 29 '20 at 21:52

1 Answers1

0

Create a Notifications Root object that contains a list of your Notifications, and deserialize from the root.

public class NotificationsRoot
{
    [JsonProperty("notifications")]
    public List<Notification> Notifications { get; set; } 
    public int companyNbr { get; set; } 
    public int storeNbr { get; set; } 
}

And deserialize like so...

var notifications = restClient.Execute<NotificationsRoot>(restRequest);
quaabaam
  • 1,808
  • 1
  • 7
  • 15
  • It doesn't look like the `JsonProperty` attribute is needed since the property is called `notifications` – ESG Sep 30 '20 at 00:23
  • Thanks. This helps. I get the list of Notification objects inside the NotificationsRoot object now but the data is all still null. –  Sep 30 '20 at 11:37