Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'

I am testing my Web API. Mocking the data I have this:

var objs = ((JArray)JsonConvert.DeserializeObject("{ \"PrintId\":10,\"Header\":\"header\",\"TC\":\"tc\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}")).Values<JObject>(); 

Which gives me the error:

Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'

The thing is it was working. I must have changed something, but I don't know what.

My intent is to convert this JSON object to a list of .NET objects called Print which has the fields:

PrintId Header TX CompnayRef 
6

4 Answers

Just make a class and deserialize it.

public class Print { public int PrintId { get; set; } public string Header { get; set; } public string TC { get; set; } public string CompanyRef { get; set; } } Print printObj = JsonConvert.DeserializeObject<Print>(yourJson); printObj.PrintId = //... 
6

As the message says, your object is JObject so don't cast it to JArray. Try this:

var objs = JsonConvert.DeserializeObject("{ \"PrintId\":10,\"Header\":\"header\",\"TC\":\"tc\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}"); 

Update To get a collection List<Print>, your JSON needs to be an array. Try this (I made your JSON an array and added a second object):

string json = "[{ \"PrintId\":10,\"Header\":\"header\",\"TC\":\"tc\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}" + ",{ \"PrintId\":20,\"Header\":\"header2\",\"TC\":\"tc2\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}]"; var objs = JsonConvert.DeserializeObject<List<Print>>(json); //The loop is only for testing. Replace it with your code. foreach(Print p in objs){ Console.WriteLine("PrintId: " + p.PrintId); Console.WriteLine("Header: " + p.Header); Console.WriteLine("TC: " + p.TC); Console.WriteLine("CompanyRef: " + p.CompanyRef); Console.WriteLine("=============================="); } public class Print { public int PrintId { get; set; } public string Header { get; set; } public string TC { get; set; } public string CompanyRef { get; set; } } 

Here is a fiddle.

6

Simply because { } is a jobject notation, to make it a jarray just put square brackets around it e.g. [{ }]. So in your case, just putting these two chars fixes the problem.

var objs = ((JArray)JsonConvert.DeserializeObject("[{ \"PrintId\":10,\"Header\":\"header\",\"TC\":\"tc\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}]")).Values<JObject>(); 

you can try here

Note: You said it was working before. So this string input probably is a result of a serialization. You did not have a problem before, because your input was always an array with multiple items until this one. And your serializer is probably producing a single object (output starting with '{') instead of an array (output starting with '[') for single item arrays.

serializing List with single object as JSON object not JSON array

For me I was putting empty string as an object which caused the issue, I switched to "{}" which fixed my issue

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like