1

Apolgies: I forget to mention this is for a Silverlight Solution.

A JSON string is returned from a service as:

{
   "710 HUVAL ST (N), LAFAYETTE LA":{
      "confidence":0.844,
      "fips_county":"22055",
      "country_code":"US",
      "country_code3":"USA",
      "latitude":30.234912,
      "street_address":"710 Huval St",
      "country_name":"United States",
      "longitude":-92.034597,
      "street_number":"710",
      "region":"LA",
      "street_name":"Huval St",
      "locality":"Lafayette"
   },
   "200 ASHLAND PARK, LAFAYETTE LA":{
      "confidence":0.844,
      "fips_county":"22055",
      "country_code":"US",
      "country_code3":"USA",
      "latitude":30.159882,
      "street_address":"200 Ashland Park Dr",
      "country_name":"United States",
      "longitude":-92.035342,
      "street_number":"200",
      "region":"LA",
      "street_name":"Ashland Park Dr",
      "locality":"Lafayette"
   }
}

I'm trying to deserialize it into a .NET class. However, I'm running into trouble because each object in the array (and it may be very large, I'm just showing two in the example above) looks to be a different object type to the JSON deserializer in .NET.

Using a tool like JSON to C# to generate the classes will create a class for each of the array objects, which is not ideal given that the results vary from response to response.

I cannot for the life of me figure out the appropriate class(es) to generate in .NET in order to be able to deserialize it. I don't have any control of the JSON service and I am stuck.

David
  • 17,673
  • 10
  • 68
  • 97
lcrumb
  • 51
  • 5
  • Possible duplicate http://stackoverflow.com/questions/6671972/deserializing-json-to-anonymous-object-in-c-sharp The idea isn't to deserialize the data into a concrete class but an anonymous object or similar primitive. – David Apr 03 '13 at 03:58
  • I apologize. I completely left out the fact that this is in Silverlight. – lcrumb Apr 03 '13 at 13:25
  • Perhaps this will work for you - http://www.robertsindall.co.uk/blog/how-to-convert-json-to-expandoobject/ Last time I worked with C#, I generally avoided anything that relied on class reflection for JSON (de)serialization as it seemed to flaky. – David Apr 03 '13 at 15:18

1 Answers1

1

Try using the JSON.NET package from nuget. The class Newtonsoft.Json.Linq.JObject will give you an interface similar to System.Xml.Linq.XElement that will allow you to parse the JSON without having to deserialize it into a single concrete object.

Raymond Saltrelli
  • 4,071
  • 2
  • 33
  • 52