This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 9th, 2013  |  syntax: None  |  size: 1.38 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. "location":{
  2. "address":"123 foo Street",
  3. "zip":"94102",
  4. "citycode":"usa:ca:sanfrancisco:downtown",
  5. "name":"San Francisco (Downtown)",
  6. "state":"CA",
  7. "country":"USA",
  8. "latitude":"37.7878",
  9. "longitude":"-122.4101"},
  10.        
  11. "location":[],
  12.        
  13. Cannot deserialize JSON array into type 'LocationData'
  14.        
  15. [JsonProperty(NullValueHandling = NullValueHandling.Ignore,Required=Required.AllowNull)]
  16. public LocationData location{get;set;}
  17.     ...
  18. public class LocationData  
  19.      {
  20.           public string zip { get; set; }
  21.           public string address { get; set; }
  22.           public string citycode { get; set; }
  23.           public string name { get; set; }
  24.           public string state { get; set; }
  25.           public string country { get; set; }
  26.           public decimal latitude { get; set; }
  27.           public decimal longitude { get; set; }
  28.      }
  29.        
  30. public class LocationDataConverter : JsonConverter
  31. {
  32.     public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  33.         {
  34.             if (reader.TokenType == JsonToken.StartArray)
  35.             {
  36.                 reader.Read(); //move to end array
  37.                 return null;
  38.             }
  39.  
  40.             var data = new LocationData();
  41.             serializer.Populate(reader, data);
  42.  
  43.             return data;
  44.         }
  45. }
  46.        
  47. [JsonConverter(typeof(LocationDataConverter))]
  48. public class LocationData {...}
clone this paste RAW Paste Data