Click here to Skip to main content
15,883,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I wrote an HTTPClient (.NET Core 3.x) that returns a JSON string but when I try to Deserialize it I run into issues. Issues ranging from can not deserialize array to string or the results are null.

The string itself I can view on a Quick Watch and render as JSON (looks great)

What I have tried:

var oDataRespone = Newtonsoft.Json.JsonConvert.DeserializeObject<odataresponse<my_object>>(content);

JArray array = (JValue)content.[0];
var Docs = Newtonsoft.Json.JsonConvert.DeserializeObject<list<my_object>>(array.AsJEnumerable);

var js = new JsonSerializer();
var Docs = js.Deserialize(new StringReader(content), typeof(my_object_List)) as my_object_List;

List<my_object_List> Docs = JsonConvert.DeserializeObject<list<my_object_list>>(content);


internal class ODataResponse<t>
{
public T[] value { get; set; }
}

//I've tried two different vesions fo the OdataResponse class

internal class oDataResponse<t>
{
public List<t> Value { get; set; }
}
Posted
Updated 20-Jul-21 18:27pm
v2

1 solution

SOLVED

I did not want to work with an OData Json object, so I had to strip the OData header and the rest just looked like a collection of JSON docs

C#
content = httpResponse.Content.ReadAsStringAsync().Result;
var outer = Newtonsoft.Json.JsonConvert.DeserializeObject<OData<object[]>>(content);
           
for (int i = 0; i < outer.value.Length; i++)
{
Rx_Documents.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<My_Object>(outer.value[0].ToString()));
}

C#
internal class OData<T>
{
   [JsonProperty("odata.context")]
   public string Metadata { get; set; }
   public T value { get; set; }
}


I'm sure there are other ways to deserialize a multipart JSON document into a collection, I just did it this way.
 
Share this answer
 
Comments
Patrice T 20-Jul-21 22:09pm    
Accept your own solution, it will close the question as solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900