12
var client = new RestClient("http://10.0.2.2:50670/api");

var request = new RestRequest("Inventory", Method.GET);

request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

// execute the request to return a list of InventoryItem
RestResponse<JavaList<InventoryItem>> response = (RestResponse<JavaList<InventoryItem>>)client.Execute<JavaList<InventoryItem>>(request);

The content returned is a JSON string, an array of objects. The following is a short excerpt of it:

[{"Id":1,"Upc":"1234567890","Quantity":100,"Created":"2012-01-01T00:00:00","Category":"Tequila","TransactionType":"Audit","MetaData":"PATRON 750ML"},{"Id":2,"Upc":"2345678901","Quantity":110,"Created":"2012-01-01T00:00:00","Category":"Whiskey","TransactionType":"Audit","MetaData":"JACK DANIELS 750ML"},{"Id":3,"Upc":"3456789012","Quantity":150,"Created":"2012-01-01T00:00:00","Category":"Vodka","TransactionType":"Audit","MetaData":"ABSOLUT 750ml"}]

The error message:

Operation is not valid due to the current state of the object

What is wrong here? My InventoryItem has the same properties as each object in the JSON string. Am I missing a step?

Josh
  • 1,019
  • 3
  • 17
  • 32

2 Answers2

9

I suspect that SimpleJson, used in RestSharp can't deserialise to a JavaList.

First I would try deserialising to a:

List<InventoryItem>

Failing that, I recommend ServiceStack.Text - .Net's fastest JSON library; and do:

var response = client.Execute(request);
var thingYouWant = JsonSerializer.DeserializeFromString<List<InventoryItem>>(response.Content);

This is actually what I do myself.

Edit (Thank you to commentators): In newer versions this would now be:

var deserializer = new JsonDeserializer();
deserializer.Deserialize<List<InventoryItem>>(response);
manadart
  • 1,750
  • 11
  • 13
  • 1
    I have no method called DeserializeFromString<> for my JsonSerializer. I did find Deserialize<> with **JsonDeserializer** class. They may have changed it in an update since last year. – Gaʀʀʏ Dec 23 '13 at 21:02
  • `JsonDeserializer deserializer = new JsonDeserializer(); deserializer.Deserialize>(response);` – Markus Jun 23 '14 at 12:54
  • 1
    I edited my post with syntax reflecting the newer version. How about removing the downvote? – manadart Jun 24 '14 at 03:01
4

Failing w/ auto-magic casting, I use this in a pinch:

var rc = new RestClient("https://api-ssl.bitly.com");
var rr = new RestRequest("/v3/link/clicks?access_token={access_token}&link={bitlyUrl}", Method.GET);

rr.AddUrlSegment("bitlyUrl", bitlyUrl);
rr.AddUrlSegment("access_token", BityAccessToken);

var response = rc.Execute(rr);
dynamic json = Newtonsoft.Json.Linq.JObject.Parse(response.Content);
var clicks = Convert.ToInt32(json.data.link_clicks.Value);
akonsu
  • 28,824
  • 33
  • 119
  • 194
sobelito
  • 1,525
  • 17
  • 13
  • Won't work anymore; .data comes back as null, it must handle it different now. You can, however, loop through the json children and if the name (key) matches your specified one, get the .value that way. May not be the best solution but got it working in a pinch. – Ryan Feb 08 '22 at 14:14