Important Update: Community URLs redirect issues are partially resolved. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

JSON Deserialize Error

CharlesGowdy2
Contributor III

I am working with the C# sample code provided through the RSA Charge labs, and I am running into an error and I cannot seem to find the fix (I am new to APIs and C# so forgive my naivety). I haven't changed any of the code provided, just updated the resource tables with our environment information (baseURL, userID, pwd, report ID, etc.).

 

I can successfully get a session token, but when I try to search for a report by ID I get the following error:

 

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[APIBasicsLab.JSON.GetReportByIDListValue]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

 

This worked for me no problem during the lab, so I'm trying to figure out what would be different now... The solution provided by Visual Studio seems easy, but I'm not sure how to actually implement it.

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

I was finally able to figure out a solution. I forced the JsonConvert to convert all ListValue, Users, Groups, and any other multi select field types to a JSON array by adding the attribute json:Array='true' in the xml:

 

<ListValues><ListValue json:Array='true' id="65756" displayName="Under Review">Under Review</ListValue></ListValues>

 

View solution in original post

4 REPLIES 4

Anonymous
Not applicable

Charles,

 

Is this still an issue for you?

Hi Scott,

 

Yep, I am still running into this error when using the sample code provided. Seems to get tripped up trying to deserialize a values list field.

I believe I have identified the issue, but not yet the solution. When the xmlDoc object is being converted to a JSON object using JsonConvert.SerializeXMLNode(xmlDoc), if a values list field has only one value selected it is not being converted into a JSON Array:

 

"Field":[{"id":"1946","guid":"184dfbaa-467d-4523-b8a9-b4a3996cc1b0","type":"4",

   "ListValues":

      {"ListValue":{"id":"1455","displayName":"Yes","text":"Yes"}

      }

}.......

 

"ListValue" should be an array but it is missing the [] surrounding it and its attributes, like how "Field" is captured as an array. If a values list field in the report does have multiple values it is translated correctly to an array and it deserializes properly:

 

"ListValues":

   {"ListValue":[{"id":"65787","displayName":"February","text":"February"},{"id":"65788","displayName":"March","text":"March"},{"id":"65789","displayName":"April","text":"April"},{"id":"65791","displayName":"June","text":"June"}]

   }

I was finally able to figure out a solution. I forced the JsonConvert to convert all ListValue, Users, Groups, and any other multi select field types to a JSON array by adding the attribute json:Array='true' in the xml:

 

<ListValues><ListValue json:Array='true' id="65756" displayName="Under Review">Under Review</ListValue></ListValues>