John Riker

John Riker

  • NA
  • 85
  • 16.1k

Changing JSON from Source

Dec 10 2020 10:40 AM
I have a program running that's been flawless up until now.  I pull several data feeds from the same vendor and they were all the same.  Now it seems for some reason there is one field that was a JSON array and now isn't for one of the feeds but hte rest are still the same.  And on top of it, that feed changes from pull to pull.  No idea why.
 
Anywho, is there a way to deal with this without creating a whole separate set of classes to deal with this and doubling my processing to use one group or another? 

So essentially I took the JSON and pated as JSON into my program.  The part that's an issue is:
 
  1. public class Regionalratings  
  2. {  
  3.     public string region { get; set; }  
  4.     public string rating { get; set; }  
  5.     public object disclaimer { get; set; }  
  6.     public Subrating[] subratings { get; set; }  
  7.     public object consumerAdvice { get; set; }  
  8.     public object ratingIcon { get; set; }  
  9. }  
 
 
it's the Subrating[].  Sometimes it needs to be Subrating1[] and sometimes Subrating1
 
The error I get is:
 
  1. Unhandled Exception: Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ShowWebsiteDownloader.Regionalratings' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.  
  2. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.  
  3. Path 'result.data[1].regionalRatings', line 1, position 4570.  
 
Funny part is I have no need for that field physically and it's usually empty.  Not sure if there even though would be dangerous, a way to fill the data as it can and skip what it can't. 
 
Call it's failing on is:
 
  1. ShowWebsiteDownloader.Rootobject rootObject = JsonConvert.DeserializeObject(rawJSON);  
 
 

Answers (1)