Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing list of JSON objects in WebApi method
    primarykey
    data
    text
    <p>I'm trying to work out how to extract a List of objects from a JSON request in a WEBAPI call. Within this list of objects is a Dictionary object, a List object and a List> object.</p> <p>My request from my MVC controller, looks a bit like this:</p> <pre><code>//Gets a list of various objects List&lt;object&gt; values = new List&lt;object&gt;(); values.add(GetItem1()); //Returns a Dictionary&lt;string, string&gt; object values.add(GetItem2()); //Returns a List&lt;string&gt; object values.add(GetItem3()); //List&lt;KeyValuePair&lt;string, string&gt;&gt; var content = new StringContent(JsonConvert.SerializeObject(values), Encoding.UTF8, "application/json"); HttpResponseMessage postResult = client.PostAsync(baseURL, content).Result; </code></pre> <p>In the WEBAPI object, I have this function that is receiving the call from the MVC controller. This is where I have my trouble, I can't get the data out of the List. The first item in the list, happens to be a Dictionary.</p> <pre><code>[HttpPost] public void LogError(List&lt;object&gt; myObjectList) { Dictionary&lt;string, string&gt; items = (Dictionary&lt;string, string&gt;)myObjectList[0]; List&lt;string&gt; moreItems = (List&lt;string&gt;)myObjectList[1]; List&lt;KeyValuePair&lt;string, string&gt;&gt; evenMoreItems = (List&lt;KeyValuePair&lt;string, string&gt;&gt;)myObjectList[2]; } </code></pre> <p>If I try to start using the object, I get the error:</p> <pre><code>Cannot cast 'myObjectList[0]' (which has an actual type of 'Newtonsoft.Json.Linq.JObject') to 'System.Collections.Generic.Dictionary&lt;string,string&gt;' </code></pre> <p>Unfortunately, I'm at a complete loss about what to do next. I can't seem to find a way to deserialize the List into these individual object types. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload